# Algorithm for sublist in python

**URL:** <https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593>\
**Category:** Developers\
**Created:** [July 31, 2017, 2:23pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593 "2017-07-31T14:23:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Villarroel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jorge_villarroel/32/23619_2.png) [@Jorge\_Villarroel](https://forum.dynamobim.com/u/Jorge_Villarroel)\
**Post date:** [July 31, 2017, 2:23pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/1 "2017-07-31T14:23:25Z")

</div>

Hi All,

I have and issue creating sub-lists in python. Here is my original list:

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

and I want to get the first and last index of every sublist, acording the indices from the original list. Here’s an image to illustrate this:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/f/ff707f06346381c54e37880526f5202365160223.jpg)

This is a part a python code. I’m not working with OOTB nodes.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Giovanni\_Brogiolo](https://avatars.discourse-cdn.com/v4/letter/g/a9adbd/32.png) [@Giovanni\_Brogiolo](https://forum.dynamobim.com/u/Giovanni_Brogiolo)\
**Post date:** [July 31, 2017, 2:37pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/2 "2017-07-31T14:37:46Z")

</div>

Hi Jorge,

Try to append the values in a list inside the loop. (I took this example from an old post on this forum).  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/d/0d2ab017493234f911bec74d0767a009c899678f.png)

Or something like this:

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/8/6/8629bd34099f1af3003b7072bf7ac9ad1cdda0ad.png)

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [July 31, 2017, 2:42pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/3 "2017-07-31T14:42:02Z")

</div>

There’s probably a cleaner way of doing this but a simple use of append will work.

```
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference('DSCoreNodes')
from DSCore import *
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
items = IN[0]
inlist = IN[1]
newlist = []

for indices in inlist:
	sublist = []
	for i in indices:
		sublist.append(items[i])
	newlist.append(sublist)

#Assign your output to the OUT variable.
OUT = newlist
```

---

<div class="post-metadata">

**Author:** ![Jorge\_Villarroel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jorge_villarroel/32/23619_2.png) [@Jorge\_Villarroel](https://forum.dynamobim.com/u/Jorge_Villarroel)\
**Post date:** [July 31, 2017, 2:50pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/4 "2017-07-31T14:50:14Z")

</div>

Thanks both @Nick_Boyts and @Giovanni_Brogiolo for the response. I’m creating list using append, but I’m looking a bit further. I can’t manage to get the algorith to do the task mentioned above.

I could get the indices from the “existe” and “no existe” elements, but I need to get the indices in the same list.

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

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

```
inic_tramo =[]
for i in range(len(tras_arriba)):
    aux = []
    for j in range(len(tras_arriba[i])):
        if tras_arriba[i][j] == 'existe':
            aux.append(j)
        else:
            j = j+1
    inic_tramo.append(aux)

fin_tramo = []
for i in range(len(tras_arriba)):
    aux = []
    for j in range(len(tras_arriba[i])):
        if tras_arriba[i][j] == 'no existe':
            aux.append(j)
        else:
            pass
    fin_tramo.append(aux)

OUT = (inic_tramo,fin_tramo)
```

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [July 31, 2017, 2:55pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/5 "2017-07-31T14:55:52Z")

</div>

OK, I think I understand now. So your sublist of indices will be the first “existe” and the first “no existe” the list encounters, then you begin a new sublist doing the same thing. Should be doable. I’ll play around with it.

---

<div class="post-metadata">

**Author:** ![Jorge\_Villarroel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jorge_villarroel/32/23619_2.png) [@Jorge\_Villarroel](https://forum.dynamobim.com/u/Jorge_Villarroel)\
**Post date:** [July 31, 2017, 2:57pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/6 "2017-07-31T14:57:28Z")

</div>

Exactly!

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [July 31, 2017, 3:00pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/7 "2017-07-31T15:00:02Z")

</div>

Is your end goal to get the indices or to convert your list into sublists based on those indices?

---

<div class="post-metadata">

**Author:** ![Jorge\_Villarroel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jorge_villarroel/32/23619_2.png) [@Jorge\_Villarroel](https://forum.dynamobim.com/u/Jorge_Villarroel)\
**Post date:** [July 31, 2017, 3:02pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/8 "2017-07-31T15:02:25Z")

</div>

The goal is to get the indices, so I was thinking of a sublist containing the first and the last item of every sequence “existe, existe…,no existe”.

---

<div class="post-metadata">

**Author:** ![Jorge\_Villarroel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jorge_villarroel/32/23619_2.png) [@Jorge\_Villarroel](https://forum.dynamobim.com/u/Jorge_Villarroel)\
**Post date:** [July 31, 2017, 3:03pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/9 "2017-07-31T15:03:49Z")

</div>

If there is a sequence with only a “no existe”, this sublist contains only one item.Like the case of the elements 7,8 and 11 from the original list.

---

<div class="post-metadata">

**Author:** ![Jorge\_Villarroel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jorge_villarroel/32/23619_2.png) [@Jorge\_Villarroel](https://forum.dynamobim.com/u/Jorge_Villarroel)\
**Post date:** [July 31, 2017, 3:42pm UTC](https://forum.dynamobim.com/t/algorithm-for-sublist-in-python/13593/10 "2017-07-31T15:42:16Z")

</div>

nailed it!

```
resultado = []
for i in range(len(tras_arriba)):
    aux = []
    resp = []
    for j in range(0,len(tras_arriba[i])):
        if tras_arriba[i][j] == 'existe':
            aux.append(j)
        else:
            aux.append(j)
            resp.append(aux)
            aux = []

    resultado.append(resp)

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/e/5/e53b3e1f43e3eeebba31372b55ba0441342d0583.png)
