# Nested Map. Iteration over a list

I am trying to add an element to a nested list. An example of the nested list is given below:

cordxy = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}}


I would like to add an element to the elements of the list without changing the structure of the list. Basically, the result I want is (assuming the extra element is a):

{{{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2, a}}}


I have found a way of doing so by mapping a Map function (or Nest-ing a Map):

Map[Map[Insert[#, a, {3}] &, #] &, cordxy]


I would like to know if there is better way of doing it.

An alternative to Map:

Replace[cordxy, {x_, y_} :> {x, y, a}, {2}]

{{{1, 2, a}, {3, 4, a}, {5, 6, a}},
{{7, 8, a}, {9, 10, a}, {1, 2, a}}}


Or alternatively one can use ReplaceAll with an appropriate pattern; this would avoid the need to specify an explicit replacement level:

cordxy /. {x_?NumberQ, y_} :> {x, y, a}

• I was also looking for a rule based method. Although I think this is not as effecient as Map. I will investigate it.
– Drod
Mar 1, 2021 at 9:15
• @Drod glad it helps, and thank you for the accept! Since you are interested in replacement based solutions, I added another approach using a replacement with a restricted pattern. Mar 1, 2021 at 13:27
Map[Append[a], cordxy, {2}]

{{{1, 2, a}, {3, 4, a}, {5, 6, a}},
{{7, 8, a}, {9, 10, a}, {1, 2,  a}}}


and

Map[Map @ Append @ a] @ cordxy

{{{1, 2, a}, {3, 4, a}, {5, 6, a}},
{{7, 8, a}, {9, 10, a}, {1, 2, a}}}

• Why are these thing so simple when you see then?! Thanks kglr
– Drod
Feb 28, 2021 at 21:44

Here is an alternative way.

PadRight[#, Dimensions@# + {0, 1}, a] & /@ cordxy

{{{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2, a}}}

• No need to map - just use PadRight directly...
– ciao
Mar 1, 2021 at 1:02
• @ciao Sorry, I don't get it. Can you please be more clear.. Mar 1, 2021 at 2:56
• I think PadRight[{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}}, {Automatic, Automatic, 3}, a] is what @ciao had in mind. Mar 1, 2021 at 5:08
• @OkkesDulgerci: For example, PadRight[#, Dimensions[#] // # + UnitVector[Length@#, Length@#] &, #2] & will pad lowest level, regardless of depth. (Called with original array and desired pad element).
– ciao
Mar 1, 2021 at 7:14
• @ciao PadRight[#, Dimensions[#] // # + UnitVector[Length@#, Length@#] &, a] &@cordxy works for the list above but if we have cordxy = {{{1, 2, 2}, {3, 4, 2}, {5, 6, 2}}, {{7, 8}, {9, 10}, {1, 2}}}; will not work? Mar 1, 2021 at 14:27

Or you can use ReplaceRepeated

{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}} //. {x_?NumericQ, y_} :> {x, y, a}

list = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}};


Using ReplaceAll

list /. x : {__?AtomQ} :> Append[x, a]


{{{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2, a}}}

Using Cases

Cases[list, x : {__} :> Append[a] /@ x]


{{{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2, a}}}

list = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}};


Using Thread:

Append @@@ Thread[{#, a}] & /@ list

(*{{{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2, a}}}*)

cordxy = {{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}};

cordxy /. x_?VectorQ :> Join[x, {a}]


{{{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2,
a}}}

ArrayFlatten[{{#, a}}] & /@ cordxy

(* {{1, 2, a}, {3, 4, a}, {5, 6, a}}, {{7, 8, a}, {9, 10, a}, {1, 2, a}} *)