# Replace part of a list using 'ReplacePart'

I have two lists, List0 and List1 that have specific structures. I want to replace the second number in each pair of numbers in List0 with subsequent numbers from a flattened version of List1. This can be achieved easily using ReplacePart for a single value from List1 but I haven't succeeded in using subsequent values.

list0 = {{{12, 34}, {34, 56}}, {{56, 78}, {78, 91}}}; list1 = {{270, 271}, {272, 273}}; list2 = Flatten[list1]; ReplacePart[list0, {_, _, 2} -> list2 ???]

The final list would take the form:

{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}

I suspect that this is quite straightforward but would appreciate an efficient means of accomplishing the task as I have a very great number of these operations to perform.

ReplacePart[list0, {_, _, 2} :> Last[list2 = RotateLeft[list2]] ]

{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}


Alternatively,

k = 1; ReplacePart[list0, {_, _, 2} :> list2[[k++]]]

 {{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}


Using Part assignment:

list0 = {{{12, 34}, {34, 56}}, {{56, 78}, {78, 91}}};
list1 = {{270, 271}, {272, 273}};

list0[[All, All, 2]] = list1;
list0

{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}


MapAt[Last[list2 = RotateLeft[list2]] &, {All, All, 2}] @ list0

k = 1; MapAt[list2[[k++]] &, {All, All, 2}] @ list0

SubsetMap[list2 &, {All, All, 2}] @ list0

• Thank you so much kglr. Commented Feb 7, 2022 at 9:36

Replace with list1

If it permitted not to Flatten list1:

ReplacePart[list0, {i_,j_,2} :> list1[[i,j]]]

(* {{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}} *)


Replace with list2

If the starting point is a flattened list (list2):

ReplacePart[list0, {i_,j_,2}:> Partition[list2,2][[i,j]]]

(* {{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}} *)


Edit

As pointed out by kglr in a comment, a better way of replacing with list2 is the following:

ReplacePart[list0, {i_, j_, 2} :> list2[[2 (i - 1) + j]]]

(* {{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}} *)


Lists

list0
list1
list2

(* {{{12, 34}, {34, 56}}, {{56, 78}, {78, 91}}} *)

(* {{270, 271}, {272, 273}} *)

(* {270, 271, 272, 273} *)

• Thank you. It will be interesting to compare the compute speed of these methods. The first by @kglr works perfectly but computation is slow (M1 iMac, 16GB, Mac OS 12.2). I'll run a comparison on the actual data set of 76,800 points. Commented Feb 8, 2022 at 7:07
• That will be interesting. I suspect that kglr's second method will be the fastest by quite a bit, but are you OK with modification in place, or do you require a new list (keeping list0 unmodified)? Maybe something like list0.{{1,0},{0,0}}+MapThread[List,{ConstantArray[0,Dimensions[#]],#},2]&[list1]? Commented Feb 8, 2022 at 13:40
• (+1) you can also do ReplacePart[list0, {i_, j_, 2} :> list2[[2 (i - 1) + j]]]
– kglr
Commented Feb 8, 2022 at 16:28
• Thank you all. I'll try the options and report back. Commented Feb 9, 2022 at 4:25

Using ArrayReduce:

list0 = {{{12, 34}, {34, 56}}, {{56, 78}, {78, 91}}};
list1 = {{270,
271}, {272, 273}};

ArrayReduce[Sequence, {First /@ list0, list1}, 1]


Result:

{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}

list0 = {{{12, 34}, {34, 56}}, {{56, 78}, {78, 91}}}; list1 = {{270, 271}, {272, 273}};


Another way using Thread:

Map[#[[{1, -1}]] &@*Flatten, Thread /@ Thread[{list0, list1}], {2}]

(*{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}*)

la = {{{12, 34}, {34, 56}}, {{56, 78}, {78, 91}}};

lb = {{270, 271}, {272, 273}};


Define replacement pattern:

p = {{{a_, _}, {b_, _}}, {c_, d_}} :> {{a, c}, {b, d}};


Using Replace

Replace[Transpose[{la, lb}], p, {1}]


{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}

Using Cases

Cases[p] @ Transpose[{la, lb}]


{{{12, 270}, {34, 271}}, {{56, 272}, {78, 273}}}