Merge sublists with common 0 index

I have two sublists with the same 0 index. I’d like to merge them into one sublist, as shown here. Driving me crazy!

This method works in python, I’ll try to see if I can do a node version:

datalist = IN[0]
data_dict = dict()
for sub in datalist:
	if sub[0] in data_dict:
		[data_dict[sub[0]].append(d) for d in sub[1:]]
	else:
		data_dict[str(sub[0])] = sub[1:]

newdata = []
for i,sub in enumerate(datalist):
	try:
		newdata.append(data_dict.pop(str(sub[0])))
		newdata[i].insert(0, sub[0])
	except KeyError:
		continue

OUT = newdata

1 Like

That would be great. I haven’t used Python code yet.

It is pretty easy if you just need to add in one node. Just create a python script node, double click it to enter, delete everything that is premade in it, and copy and paste what I typed above into it.

Here’s a node solution using GroupByKey to separate the first item and group the second items.

3 Likes

Nick, this is awesome. It works. I haven’t used the nodes List.Deconstruct or List.GroupByKey before. Need to get up to speed on “keys”. Very educational - thank you!

I tried the Python code, but got 0 for a result. I copied and pasted as you suggested. Hmm. Nick_Boyts solved it using nodes, so I’m good to go. Thanks for the input.

FYI 0 is the default output. You probably missed something in the code.

It looks like you double clicked the title of the block, not the block itself. Double click the beige part.

1 Like