# Insert element into a sublist?

**URL:** <https://community.wolfram.com/t/insert-element-into-a-sublist/13297>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [November 11, 2018, 4:28pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297 "2018-11-11T16:28:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![drkerr](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@drkerr](https://community.wolfram.com/u/drkerr)\
**Post date:** [November 11, 2018, 4:28pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/1 "2018-11-11T16:28:17Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jashonder](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jashonder/32/26540_2.png) [@jashonder](https://community.wolfram.com/u/jashonder)\
**Post date:** [November 11, 2018, 5:08pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/2 "2018-11-11T17:08:23Z")

</div>

Use `Append`:

```
Map[Append[#, 10]&, list]

```

---

<div class="post-metadata">

**Author:** ![drkerr](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@drkerr](https://community.wolfram.com/u/drkerr)\
**Post date:** [November 11, 2018, 6:45pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/3 "2018-11-11T18:45:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kmartin](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kmartin/32/38196_2.png) [@kmartin](https://community.wolfram.com/u/kmartin)\
**Post date:** [November 11, 2018, 7:42pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/4 "2018-11-11T19:42:47Z")

</div>

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}}

---

<div class="post-metadata">

**Author:** ![drkerr](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@drkerr](https://community.wolfram.com/u/drkerr)\
**Post date:** [November 11, 2018, 10:15pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/5 "2018-11-11T22:15:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [November 12, 2018, 11:13am UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/6 "2018-11-12T11:13:50Z")

</div>

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 /@ {%, %%}

```

![ert3456w.png](https://global.discourse-cdn.com/wolfram/original/3X/c/d/cd27107b44e33389e5533e99076f5a6452ee5979.png)

---

<div class="post-metadata">

**Author:** ![gdelfino](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/gdelfino/32/41294_2.png) [@gdelfino](https://community.wolfram.com/u/gdelfino)\
**Post date:** [November 12, 2018, 7:23pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/7 "2018-11-12T19:23:17Z")

</div>

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"}}

```

---

<div class="post-metadata">

**Author:** ![drkerr](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@drkerr](https://community.wolfram.com/u/drkerr)\
**Post date:** [November 12, 2018, 7:32pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/8 "2018-11-12T19:32:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [November 12, 2018, 8:47pm UTC](https://community.wolfram.com/t/insert-element-into-a-sublist/13297/9 "2018-11-12T20:47:14Z")

</div>

[@Gustavo Delfino](http://community.wolfram.com/web/gdelfino), 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.
