Add children as sublist to parent

Hello all,

I currently have this list:

If the list item contains “AcDbPolyline” I need to add it as a sublist to the parent above it. How can this be done? Indices of the parent may vary and the amount of children can also vary.

Greetings,
Stan

in your graph looks everything fine… can show what you need to achieve?

I want to achieve the following list structure;
if item contains “AcDbPolyline” -> move item as sublist to the item above it.

So the end result should look like;
List
0 AcDbZombieEntity
1 AcDbZombieEntity
list 0
0 AcDbPolyLine
1 AcDbPolyLine
2 AcDbZombieEntity
list 0
0 AcDbPolyLine
1 AcDbPolyLine`

Hi @stabouw,
Try this

#Code from https://stackoverflow.com/questions/1198512/split-a-list-into-parts-based-on-a-set-of-indexes-in-python

def partition(alist, indices):
    return [alist[i:j] for i, j in zip([0]+indices, indices+[None])]
    
OUT = partition(IN[0],IN[1])
3 Likes

Thanks! Works like a charm. I should someday further dive into the power of Python.

2 Likes

You could also consider exploring the power of Design Script :wink:

bln2 = List.Sublists(bln1,0..1,1);
bln3 = List.AllFalse(bln2<1>)?false:List.LastItem(bln2<1>)==true?true:List.FirstItem(bln2<1>)==true?false:true;
ind1 = List.Flatten([0,List.AllIndicesOf(bln3,false)+1,List.Count(bln1)],-1);
ind2 = List.RestOfItems(ind1)-List.DropItems(ind1,-1);
lst1 = List.Chop(lst,ind2);

chopByBoolSeq.dyn (8.0 KB)

4 Likes