# How to replace one part of list of lists

I have the following data and want to replace only the first part in all sublists by the square root. It can be done by:

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};
t2 = Transpose[{Sqrt /@ data[[All, 1]]}];
t3 = Table[Flatten[AppendTo[t2[[i]], data[[i, 2 ;; 3]]]], {i, 3}]


and I get

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}


What is a shorter way using Replace or other methods (and/or using patterns)?

• This data /. {a_Integer, b_Integer, c_Integer} :> {Sqrt[a], b, c} seems to satisfy your immediate requirement. Is that the kind of answer you want ? Mar 30, 2020 at 16:15
• {Sqrt[#[[1]]], #[[2]], #[[3]]} & /@ data or {Sqrt[#[[1]]], Sequence @@ Rest@##} & /@ data or ReplacePart[#, 1 -> Sqrt[#[[1]]]] & /@ data Mar 30, 2020 at 16:32
• data[[;; , 1]] = Sqrt[data[[;; , 1]]] Mar 30, 2020 at 16:36
• data[[All, 1]] = Sqrt[data[[All, 1]]] . Mar 30, 2020 at 16:44

Here are a few ways:

MapAt[Sqrt, data, {All, 1}]

Replace[data, {x_, y___} :> {Sqrt[x], y}, {1}]

data // Query[All, {1 -> Sqrt}]

SubsetMap[Sqrt, data, {All, 1}]

ReplacePart[data, {i_, 1} :> Sqrt[data[[i, 1]]]]

data2 = data;
data2[[All, 1]] = Sqrt[data2[[All, 1]]];
data2

• Wow! That gives me material to breed over. Great! Mar 30, 2020 at 16:52
☺ = {#^(1/2), ##2} &;

☺ @@@ data

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}


or

☺☺ = {#^(1/2), ##2} & @@@ # &;

☺☺ @ data

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

• I can understand your first solution, but what does "@@@ # &" in the second part really mean? Mar 31, 2020 at 4:21
• @user57467,  foo@@@#& replaces heads at level 1 of input expression with foo (see Apply)
– kglr
Mar 31, 2020 at 4:52

What kglr posted, but a bit more "golfed"

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};

{√#, ##2} & @@@ data

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

• I haver never seen this symbol. Looks good! :) Mar 31, 2020 at 4:25
data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};


Using Distribute:

Distribute[data, Function[n, {Sqrt@n[[1]], Splice@n[[2 ;;]]}] /@ # &]


Result:

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

list = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};


Showing some of the newer functions

SubsetMap[Sqrt, {1}] /@ list

ReplaceAt[x_ :> Sqrt[x], {All, 1}] @ list

SequenceReplace[{{x_, y__}} :> {Sqrt[x], y}] @ list


All return

{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}


Sjoerd Smit has posted a very neat method for applying a function to a matrix column:

data = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};

Query[All,{1->(Sqrt[#]&)}]@data

(* {{7,35,14},{8,40,16},{9,45,18}} *)


In this instance, Query is using MapAt 'under the hood':

Query[All,{1->(Sqrt[#]&)}]//Normal

(* MapAt[Sqrt[#1]&,{All,1}] *)

• Also: Query[All, {1 -> Sqrt}]@data (already upvoted)
– eldo
May 16 at 0:09
list = {{49, 35, 14}, {64, 40, 16}, {81, 45, 18}};


Using Cases

Cases[{a_, b__} :> {Sqrt[a], b}] @ list


{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}

A variant of kglr's solution using MapApply (new in 13.1)

MapApply[{Sqrt[#1], ##2} &] @ list


{{7, 35, 14}, {8, 40, 16}, {9, 45, 18}}