Inserting element into list with python

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?

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

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

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

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

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

1 Like

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

You mean to flatten in python without the DSCore method?

yes

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()

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:

1 Like

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