Message Boards Message Boards

0
|
10098 Views
|
8 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Insert element into a sublist?

Posted 6 years ago

I can't figure out how to do the following.

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

I want to add a number to each sublist For example add 10 so it looks like:

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

I have looked Insert, Map etc but can't seem to grasp how to do it. In actuality I will want to add a unique ID (ie each unique ID is different like a primary key) to each sublist so it would be list = {{1,2,3,unique ID},{4,5,6, unique ID},{7,8,9, unique ID}} The unique ID would be most likely by a function I suppose.

Thanks!

POSTED BY: David Kerr
8 Replies

Using operators:

{{1,2,3},{4,5,6},{7,8,9}} // Map[Append[10]]

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

also

{{1,2,3},{4,5,6},{7,8,9}} // Map[Append[Hold[CreateUUID[]]]] // ReleaseHold

{{1,2,3,"2eff92b7-6f53-46f4-8547-f00d001533af"},
 {4,5,6,"0a8dcd99-b35e-47cc-b225-966fde86aaf0"},
 {7,8,9,"01fae17f-1bef-40fc-ab46-1152dce88143"}}
POSTED BY: Gustavo Delfino
Posted 6 years ago

Thank you all for the responses! This gives me a good framework for the project. CreateUUID is very helpful since that is what I need to do the list I have. All the suggestions though give me a strategies for this type of problem.

POSTED BY: David Kerr

@Gustavo Delfino, UUID idea is rather neat! A note, - in non-operator form the Hold is not needed:

Join[#, {CreateUUID[]}] & /@ {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

BTW, using Join should be faster than Append on large datasets according to some sources, but I did not check this personally.

POSTED BY: Vitaliy Kaurov

If you original array is not ragged (unequal lengths of sublists), then it is equivalent to adding row / column (a vector) to a matrix. Consider initial data:

m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
v = {a, b, c};

and these operations:

Join[m, {v}]
Join[m, Transpose@{v}, 2]

here is the matrix visual to better understand:

MatrixForm /@ {%, %%}

enter image description here

POSTED BY: Vitaliy Kaurov
Posted 6 years ago

Very nice! I am trying to do this all in Mathematica vs SQL and or Excel. So, this really helps. Otherwise I always fall back and never progress with Mathematica.

POSTED BY: David Kerr
Posted 6 years ago

Thanks so much! I spent a couple hours trying to figure this out. My error was when I used Append on the list it added it to the list and I did not get that Map effectively does that to each sublist. Append[list, 10] {{1, 2, 3}, {3, 4, 5}, 10} But by using Map it did that over the sublists. My next challenge will be to add a unique number to the lists.

POSTED BY: David Kerr
Posted 6 years ago

If you have the unique numbers in their own list, i.e. something like

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

You can use MapThread with Join or a similar functionto combine them:

MapThread[Join, {list, uniqueVals}]

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

POSTED BY: Kyle Martin

Use Append:

Map[Append[#, 10]&, list]
POSTED BY: John Shonder
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract