List Merge

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

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.

@Cesar_Escalante

Using Python, option 1:

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:

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

Here is a way just using nodes.

9 Likes

Ultra simple approach:

38 Likes

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?

How would this work with 3 lists?

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!

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])
2 Likes

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…

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

No no, it’s way simpler than that :sweat_smile:

2 Likes

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 :slightly_smiling_face:

My approach applied to the problem in the thread:

The different methods used for my script:


3 Likes

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

you’re killing it!!

simple but gold :sweat_smile: