12
$\begingroup$

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.

Thanks in advance, Drod

$\endgroup$

8 Answers 8

7
$\begingroup$

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}
$\endgroup$
2
  • $\begingroup$ I was also looking for a rule based method. Although I think this is not as effecient as Map. I will investigate it. $\endgroup$
    – Drod
    Mar 1, 2021 at 9:15
  • $\begingroup$ @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. $\endgroup$
    – MarcoB
    Mar 1, 2021 at 13:27
11
$\begingroup$
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}}}
$\endgroup$
1
  • 2
    $\begingroup$ Why are these thing so simple when you see then?! Thanks kglr $\endgroup$
    – Drod
    Feb 28, 2021 at 21:44
7
$\begingroup$

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}}}
$\endgroup$
7
  • 1
    $\begingroup$ No need to map - just use PadRight directly... $\endgroup$
    – ciao
    Mar 1, 2021 at 1:02
  • 2
    $\begingroup$ @ciao Sorry, I don't get it. Can you please be more clear.. $\endgroup$ Mar 1, 2021 at 2:56
  • 4
    $\begingroup$ 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. $\endgroup$ Mar 1, 2021 at 5:08
  • 2
    $\begingroup$ @OkkesDulgerci: For example, PadRight[#, Dimensions[#] // # + UnitVector[Length@#, Length@#] &, #2] & will pad lowest level, regardless of depth. (Called with original array and desired pad element). $\endgroup$
    – ciao
    Mar 1, 2021 at 7:14
  • $\begingroup$ @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? $\endgroup$ Mar 1, 2021 at 14:27
7
$\begingroup$

Or you can use ReplaceRepeated

{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {1, 2}}} //. {x_?NumericQ, y_} :> {x, y, a}
$\endgroup$
4
$\begingroup$
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}}}

$\endgroup$
4
$\begingroup$
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}}}*)
$\endgroup$
3
$\begingroup$
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}}}

$\endgroup$
3
$\begingroup$
ArrayFlatten[{{#, a}}] & /@ cordxy

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.