Copy/match list structure

Hi all,

I want to rearrange a flattened list into a specific sublist arrangement. I tought this would be easy-peasy but its driving me insane. What am I missing?

In this example I want to arrange the letters in the sublist to match the order of the numbers, but it really applies to any flattened list with any kind of data, the same length as a non flattened list.

Hello @CarlC

I know its a lacing problem, but i dont know how to solve using dynamo nodes,
but it can be solved using python

Copymatch list.dyn (13.9 KB)

Hi @honeyjain619

I see, seems a bit complicated! And there are two python scripts because right now the nesting go two levels deep I guess? How to achieve something more dynamic, independent of the number of nested levels, is really what I’m trying to figure out.

Allright, I found a working OOTB solution! Using add item to front and then take items. Not the most obvious solution imho…

While it is always better to retain the List Structure (where required) through the intermediate steps, you could something like this to restructure a list.
I wouldn’t encourage this approach though :smirk:

reconstructStructure.dyn (11.2 KB)

def reconstruct (lst01:var[]..[],lst02:var[]..[])
	cnt01 = List.Count(List.Flatten(lst01<1>,-1)<1>);
	cnt02 = List.Count(List.Flatten(lst01<1><2>,-1)<1><2>);
	cnt03 = List.Count(List.Flatten(lst01<1><2><3>,-1)<1><2><3>);
	cnt04 = List.Count(List.Flatten(lst01<1><2><3><4>,-1)<1><2><3><4>);
	cnt05 = List.Count(List.Flatten(lst01<1><2><3><4><5>,-1)<1><2><3><4><5>);

	chp01 = List.Chop(lst02,cnt01);
	chp02 = List.Chop(chp01<1>,cnt02<1>);
	chp03 = List.Chop(chp02<1><2>,cnt03<1><2>);
	chp04 = List.Chop(chp03<1><2><3>,cnt04<1><2><3>);
	chp05 = List.Chop(chp04<1><2><3><4>,cnt05<1><2><3><4>);

	ind01 = List.Flatten([cnt01,cnt02,cnt03,cnt04,cnt05]<1>,-1);
	ind02 = List.IndexOf(List.AllTrue((ind01==1)<1>),true)-1;

	return List.GetItemAtIndex([chp01,chp02,chp03,chp04,chp05],ind02);

It’s messy but it does what you need. I believe this will work on any structure as well.


Nice, seems not as messy as mine at least :slight_smile: