# Add children as sublist to parent

**URL:** <https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645>\
**Category:** Lists-Logic\
**Created:** [November 24, 2020, 2:49pm UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645 "2020-11-24T14:49:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stabouw](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@stabouw](https://forum.dynamobim.com/u/stabouw)\
**Post date:** [November 24, 2020, 2:49pm UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645/1 "2020-11-24T14:49:26Z")

</div>

Hello all,

I currently have this list:

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

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

---

<div class="post-metadata">

**Author:** ![\_Vijay](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/_vijay/32/91307_2.png) [@\_Vijay](https://forum.dynamobim.com/u/_Vijay)\
**Post date:** [November 24, 2020, 4:40pm UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645/2 "2020-11-24T16:40:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stabouw](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@stabouw](https://forum.dynamobim.com/u/stabouw)\
**Post date:** [November 24, 2020, 4:49pm UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645/3 "2020-11-24T16:49:30Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![AmolShah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/amolshah/32/103244_2.png) [@AmolShah](https://forum.dynamobim.com/u/AmolShah)\
**Post date:** [November 24, 2020, 4:57pm UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645/4 "2020-11-24T16:57:25Z")

</div>

Hi @stabouw,  
Try this

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/2/8/28bf47a06c253b4b1efaf95df10e0f85dbc62a41.png)

```
#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])
```

---

<div class="post-metadata">

**Author:** ![stabouw](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@stabouw](https://forum.dynamobim.com/u/stabouw)\
**Post date:** [November 24, 2020, 7:01pm UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645/5 "2020-11-24T19:01:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vikram\_Subbaiah](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/vikram_subbaiah/32/50797_2.png) [@Vikram\_Subbaiah](https://forum.dynamobim.com/u/Vikram_Subbaiah)\
**Post date:** [November 25, 2020, 4:29am UTC](https://forum.dynamobim.com/t/add-children-as-sublist-to-parent/57645/6 "2020-11-25T04:29:46Z")

</div>

> [@stabouw](#):
>
> further dive into the power of Python.

You could also consider exploring the power of Design Script 😉

 ![chopByBoolSeq_2020-11-25_09-53-19](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/1/d/1d5672a410024c4521b50e44a19eb309fe3d4dcf.png)

```
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](https://forum.dynamobim.com/uploads/short-url/5QxKsFVS6mSPzlRFmInnBkxlnzG.dyn) (8.0 KB)
