# List Merge

**URL:** <https://forum.dynamobim.com/t/list-merge/8918>\
**Category:** Lists-Logic\
**Created:** [January 17, 2017, 3:14pm UTC](https://forum.dynamobim.com/t/list-merge/8918 "2017-01-17T15:14:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cesar\_Escalante](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cesar_escalante/32/18786_2.png) [@Cesar\_Escalante](https://forum.dynamobim.com/u/Cesar_Escalante)\
**Post date:** [January 17, 2017, 3:14pm UTC](https://forum.dynamobim.com/t/list-merge/8918/1 "2017-01-17T15:14:39Z")

</div>

Am trying to merge two list in the order displayed below. Any help will be appreciated.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/8/c81c6e37691980bc99e28755c2c4e66dda0209d1.jpg)

---

<div class="post-metadata">

**Author:** ![Kulkul](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/kulkul/32/49801_2.png) [@Kulkul](https://forum.dynamobim.com/u/Kulkul)\
**Post date:** [January 17, 2017, 3:17pm UTC](https://forum.dynamobim.com/t/list-merge/8918/2 "2017-01-17T15:17:27Z")

</div>

Hi @Cesar_Escalante

One of the possible way is to join both list then flatten then use List.Sort node and then List.Chop length by 5.

---

<div class="post-metadata">

**Author:** ![salvatoredragotta](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/salvatoredragotta/32/10078_2.png) [@salvatoredragotta](https://forum.dynamobim.com/u/salvatoredragotta)\
**Post date:** [January 18, 2017, 12:02am UTC](https://forum.dynamobim.com/t/list-merge/8918/3 "2017-01-18T00:02:01Z")

</div>

@Cesar_Escalante

Using Python, option 1:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/3/5/3513bd78d6d598abf7b6b5bb741e874ab2b79f48.png)

```
def MergeSort(lst1,lst2):
	MergedList=[]
	for index,i in enumerate(lst1):
		MergedList.append(i+lst2[index])
		MergedList[index].sort()
	return MergedList

OUT=MergeSort(IN[0],IN[1])

```

Option 2:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/1/e1b10f5d97e5ccf2b4c3280c604104bb9f3bd048.png)

```
MergedList=[i+IN[1][index] for index,i in enumerate(IN[0])]
for i in MergedList:
	i.sort()
OUT=MergedList
```

---

<div class="post-metadata">

**Author:** ![Scott\_Crichton](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/scott_crichton/32/8876_2.png) [@Scott\_Crichton](https://forum.dynamobim.com/u/Scott_Crichton)\
**Post date:** [January 18, 2017, 11:43pm UTC](https://forum.dynamobim.com/t/list-merge/8918/4 "2017-01-18T23:43:28Z")

</div>

Here is a way just using nodes.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/a/2af3bc7ead2ed9c38a6047257d5c30dfb1cb483d.png)

---

<div class="post-metadata">

**Author:** ![Thomas\_Mahon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_mahon/32/64209_2.png) [@Thomas\_Mahon](https://forum.dynamobim.com/u/Thomas_Mahon)\
**Post date:** [January 19, 2017, 12:36am UTC](https://forum.dynamobim.com/t/list-merge/8918/5 "2017-01-19T00:36:34Z")

</div>

Ultra simple approach:

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/1/a1bf01d46d29dd1255ea8a335a22cefb757e9767.jpg)

---

<div class="post-metadata">

**Author:** ![332205](https://avatars.discourse-cdn.com/v4/letter/3/ecd19e/32.png) [@332205](https://forum.dynamobim.com/u/332205)\
**Post date:** [August 23, 2019, 6:39am UTC](https://forum.dynamobim.com/t/list-merge/8918/6 "2019-08-23T06:39:17Z")

</div>

Hey everyone,

I am having the same question about merging lists. Only my list does not consit of a,b,c,d or 1,2,3,4 but random values like d,y,h,w which can’t be sorted, because I need the original order - any ideas?

---

<div class="post-metadata">

**Author:** ![J-estrada01](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@J-estrada01](https://forum.dynamobim.com/u/J-estrada01)\
**Post date:** [September 16, 2019, 2:54am UTC](https://forum.dynamobim.com/t/list-merge/8918/7 "2019-09-16T02:54:00Z")

</div>

> [@salvatoredragotta](#):
>
> MergedList=[i+IN[1][index] for index,i in enumerate(IN[0])] for i in MergedList: i.sort() OUT=MergedList

How would this work with 3 lists?

---

<div class="post-metadata">

**Author:** ![mario.trabucchi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mario.trabucchi/32/108531_2.png) [@mario.trabucchi](https://forum.dynamobim.com/u/mario.trabucchi)\
**Post date:** [February 22, 2021, 9:43am UTC](https://forum.dynamobim.com/t/list-merge/8918/8 "2021-02-22T09:43:13Z")

</div>

> [@salvatoredragotta](#):
>
> ```auto
> def MergeSort(lst1,lst2):
> MergedList=[]
> for index,i in enumerate(lst1):
> MergedList.append(i+lst2[index])
> MergedList[index].sort()
> return MergedList
> 
> OUT=MergeSort(IN[0],IN[1])
> 
> ```

Hello @Thomas_Mahon just wondering… How comes that in your List.Join you can choose to work with levels of the lists? I can’t! Is that an OOTB Dynamo node or…?

Thank you!

---

<div class="post-metadata">

**Author:** ![Thomas\_Mahon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_mahon/32/64209_2.png) [@Thomas\_Mahon](https://forum.dynamobim.com/u/Thomas_Mahon)\
**Post date:** [February 22, 2021, 11:14am UTC](https://forum.dynamobim.com/t/list-merge/8918/9 "2021-02-22T11:14:49Z")

</div>

Hi @mario.trabucchi its purely a developer decision in respect to your list structure and Dynamo follows the norms in that respect. In general (this applies, more so for visual programming), you want to preserve your list structure so in the example above thats why the 2d list structure persists - input is 2D so the output should be 2D.

In Python its a lot more dynamic so I would avoid primitive obsession (over dependency on primitive types, lists, dictionaries etc) and declare your own class so you can handle your data structures more robustly as the following will fail if the user provides two lists which do not have the same number of sublists:

```
def MergeSort(listsA, listsB):

	mergedLists = []
	for i, subListA in enumerate(listsA):
		mergedList = subListA + listsB[i]

		mergedList.sort()

		mergedLists.append(mergedList)

	return mergedLists

OUT = MergeSort(IN[0],IN[1])
```

---

<div class="post-metadata">

**Author:** ![mario.trabucchi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mario.trabucchi/32/108531_2.png) [@mario.trabucchi](https://forum.dynamobim.com/u/mario.trabucchi)\
**Post date:** [February 22, 2021, 11:31am UTC](https://forum.dynamobim.com/t/list-merge/8918/10 "2021-02-22T11:31:15Z")

</div>

Thank you very much for your reply, but I was referring to the Dynamo node i saw in your screen capture (honestly I don’t know how this piece of code got sticked in here…).  
I am just a beginner in Dynamo so scripting is still a quite far objective for me actually…

---

<div class="post-metadata">

**Author:** ![Thomas\_Mahon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_mahon/32/64209_2.png) [@Thomas\_Mahon](https://forum.dynamobim.com/u/Thomas_Mahon)\
**Post date:** [February 22, 2021, 5:19pm UTC](https://forum.dynamobim.com/t/list-merge/8918/11 "2021-02-22T17:19:37Z")

</div>

The reason why the nodes in my example operate at the second dimension is because of two things:

1. Dynamo’s in-built replication - this is essentially a mechanism which enables dynamo to iterate over collections of objects like a for loop without ever needing to declare one.
2. The list combine node coupled with the `List.Join` combinator. This node, or rather nodes which allow for unresolved nodes (light grey title bar) to be provided as an input, is a novelty of Dynamo whereby unconnected nodes returns the function of itself. This function can then be passed as an input into another node. So the `List.Combine` applies the `List.Join` node as its performing replication (iterating over) on each sub-list {“a”, “c”, “e”} and {“f”, “h”, “j”} with each of its inputs.

The result is {“a”, “c”, “e”} + {“b”, “d”} and {“f”, “h”, "j} + {“g”, “i”} which due to the replication all get collated into a list and returned, hence a list of lists as the result:  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/4/d46565b6fa4e03410267340d2238e753797a0e55.png)

---

<div class="post-metadata">

**Author:** ![mario.trabucchi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mario.trabucchi/32/108531_2.png) [@mario.trabucchi](https://forum.dynamobim.com/u/mario.trabucchi)\
**Post date:** [March 13, 2021, 3:18pm UTC](https://forum.dynamobim.com/t/list-merge/8918/12 "2021-03-13T15:18:21Z")

</div>

No no, it’s way simpler than that 😅

 ![a](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/0/0019f09c2bcba8198e8fc1134041f2c93bf7b83b.jpeg)

---

<div class="post-metadata">

**Author:** ![scott.tomlinson](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@scott.tomlinson](https://forum.dynamobim.com/u/scott.tomlinson)\
**Post date:** [March 14, 2021, 10:57am UTC](https://forum.dynamobim.com/t/list-merge/8918/13 "2021-03-14T10:57:47Z")

</div>

> [@Thomas\_Mahon](#):
>
> Ultra simple approach:

Hi. I was interested in this for a script I’ve been working on… I knew there must have been a neater solution to mine. List.Join + List.Combine worked but interestingly added 20+ seconds of processing time over the method I’d used previously. Just FYI 🙂

My approach applied to the problem in the thread:

 ![test_2021-03-14_10-37-58](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/2/c299942b812442f1814094e26774703458ce6968.png)

The different methods used for my script:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/0/9036737f5bb26d9b29102dac7c6260546001a1bf.png)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/5/55d07de7fa91d1aecd0d08a4b52fc70672faeaee.png)

---

<div class="post-metadata">

**Author:** ![jeremy.colombe](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jeremy.colombe/32/117329_2.png) [@jeremy.colombe](https://forum.dynamobim.com/u/jeremy.colombe)\
**Post date:** [February 2, 2022, 8:14pm UTC](https://forum.dynamobim.com/t/list-merge/8918/14 "2022-02-02T20:14:45Z")

</div>

This is gold! Exactly what I needed and love keeping it simple

---

<div class="post-metadata">

**Author:** ![Dario\_Castellani\_T](https://avatars.discourse-cdn.com/v4/letter/d/a88e57/32.png) [@Dario\_Castellani\_T](https://forum.dynamobim.com/u/Dario_Castellani_T)\
**Post date:** [March 30, 2022, 2:56pm UTC](https://forum.dynamobim.com/t/list-merge/8918/15 "2022-03-30T14:56:42Z")

</div>

you’re killing it!!

---

<div class="post-metadata">

**Author:** ![lourdesmarcano](https://avatars.discourse-cdn.com/v4/letter/l/a3d4f5/32.png) [@lourdesmarcano](https://forum.dynamobim.com/u/lourdesmarcano)\
**Post date:** [June 16, 2023, 7:48am UTC](https://forum.dynamobim.com/t/list-merge/8918/16 "2023-06-16T07:48:29Z")

</div>

simple but gold 😅
