10
$\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$

4 Answers 4

4
$\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
8
$\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
4
$\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
4
$\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$

Your Answer

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

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