# Inserting element into list with python

**URL:** https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626
**Category:** Developers
**Created:** [August 1, 2017, 1:35pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626 "2017-08-01T13:35:36Z")
**Posts on this page:** 11
**Page:** 2

<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: [August 1, 2017, 4:50pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/21 "2017-08-01T16:50:29Z")

</div>

Searching into the previous lines of the code, I did this:

```
resp = []
for i in range(len(inicio)):
    resp.append(List.Flatten(inicio[i],1))
inicio = resp

```

so Inicio came formatted from this point as a list with nested lists which were **flattened**.

Could this be the seed of the problem?

---

<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: [August 1, 2017, 4:54pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/22 "2017-08-01T16:54:31Z")

</div>

Here is he **inicio** sequence for best understanding:

```
inicio = []
fin = []
for capa in segmentos:
    aux = []
    aux2 = []
    resp = []
    resp2 = []
    for tramo in capa:
        aux.append(List.FirstItem(tramo))
        aux2.append(List.LastItem(tramo))
        resp.append(aux)
        resp2.append(aux2)
        aux = []
        aux2 = []
    inicio.append(resp)
    fin.append(resp2)

resp = []
for i in range(len(inicio)):
    resp.append(List.Flatten(inicio[i],1))
inicio = resp

resp = []
for i in range(len(fin)):
    resp.append(List.Flatten(fin[i],1))
fin = resp

for k in range(len(ind_pisos)):
    for i in range(len(inicio)):
        for j in range(len(inicio[i])):
            if ind_pisos[k] > inicio[i][j] and ind_pisos[k] < fin[i][j]:
                aux = list(inicio[i])
                aux.insert(j + 1, ind_pisos[k])
                aux2 = list(fin[i])
                aux2.insert(j, ind_pisos[k])
                inicio[i] = aux
                fin[i] = aux2
            else:
                pass
```

---

<div class="post-metadata">

### Author: ![T\_Pover](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t_pover/32/19147_2.png) [@T\_Pover](https://forum.dynamobim.com/u/T_Pover)
#### Post date: [August 1, 2017, 6:58pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/23 "2017-08-01T18:58:08Z")

</div>

That could very well be. Could you show me all the import statements at the top of your code?

---

<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: [August 1, 2017, 7:45pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/24 "2017-08-01T19:45:47Z")

</div>

```
import clr
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry
from Autodesk.DesignScript.Geometry import *

clr.AddReference('DSCoreNodes')
from DSCore import *
```

---

<div class="post-metadata">

### Author: ![T\_Pover](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t_pover/32/19147_2.png) [@T\_Pover](https://forum.dynamobim.com/u/T_Pover)
#### Post date: [August 1, 2017, 8:01pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/25 "2017-08-01T20:01:54Z")

</div>

I see, so it must be the DSCore method that caused it. Oh well, glad to hear you found a workaround 🙂

---

<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: [August 1, 2017, 8:15pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/26 "2017-08-01T20:15:40Z")

</div>

Yeah!. Now I’m figuring out how to flatten a nested list to get the exact data structure, avoiding List.Flatten. Any ideas?

---

<div class="post-metadata">

### Author: ![T\_Pover](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t_pover/32/19147_2.png) [@T\_Pover](https://forum.dynamobim.com/u/T_Pover)
#### Post date: [August 1, 2017, 8:18pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/27 "2017-08-01T20:18:35Z")

</div>

You mean to flatten in python without the DSCore method?

---

<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: [August 1, 2017, 8:18pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/28 "2017-08-01T20:18:46Z")

</div>

yes

---

<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: [August 1, 2017, 8:19pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/29 "2017-08-01T20:19:58Z")

</div>

```
resp = []
for i in range(len(inicio)):
    resp.append(List.Flatten(inicio[i],1))
inicio = resp

resp = []
for i in range(len(fin)):
    resp.append(List.Flatten(fin[i],1))
fin = resp

```

this part, avoiding List.Flatten()

---

<div class="post-metadata">

### Author: ![T\_Pover](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/t_pover/32/19147_2.png) [@T\_Pover](https://forum.dynamobim.com/u/T_Pover)
#### Post date: [August 1, 2017, 8:30pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/30 "2017-08-01T20:30:55Z")

</div>

Flatten in python is actually not that easy as the depths of lists can vary a lot. So you would have to come up with some kind of recursive function to tackle every kind of depth level.  
In your case this should work I think:

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

---

<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: [August 1, 2017, 8:32pm UTC](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626/31 "2017-08-01T20:32:40Z")

</div>

@T_Pover Thanks a lot for your time! This thread really taught me a lot today!.

[Previous page](https://forum.dynamobim.com/t/inserting-element-into-list-with-python/13626.md?page=1)
