Inserting element into list with python

Hi all,

I’m having some issues with inserting an element into a list through python. I’m using the built-in function insert, but this is what I get.

Am I missing something here?

OUT = inicio[0]
image

OUT = inicio[0].insert(0,100)
image

OUT = list(inicio[0]).insert(0,100)
image

Thanks in advance!

image

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

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

lista = [0,1,2,3,4,5,6,7,8,9,10]

OUT = Insert(lista,5,100)

Don’t know what am I doing wrong?

1 Like

Hi @T_Pover:

Thanks for your response. Following your guideline I’m trying to do this into a nested list, but I get an error:

image

    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]:
                    inicio[i].insert(j+1,ind_pisos[k])
                else:
                    pass

    OUT = inicio

What’s wrong?

and this is fin:

Seems to work for me:

It might be the indentation, I had to reset all of them.

What do you mean?

I copy pasted it from your previous post and all the indentations were spaces. Maybe there was a misaligned space, so I backspaced them all and then used tab to set the indentations.

I’m using PyCharm and checked what you said. still got the error. It seems like it reads inicio[i] not as a list, but as an object?

image

Here’s redoing your example:

image

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

#The inputs to this node will be stored as a list in the IN variables.
inicio = IN[0]
ind_pisos = IN[1]
fin = IN[2]

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]:
                inicio[i].insert(j+1,ind_pisos[k])
                fin[i].inicio[i].insert(j,ind_pisos[k])
            else:
                pass
                
OUT = inicio

@T_Pover. I mis-typed the previous code:

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

#The inputs to this node will be stored as a list in the IN variables.
inicio = IN[0]
ind_pisos = IN[1]
fin = IN[2]

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]:
                inicio[i].insert(j+1,ind_pisos[k])
                fin[i].insert(j,ind_pisos[k])
            else:
                pass
                
OUT = inicio

In your example, it works fine, but in the code that I’m working with don’t.

So, my question is: I’m creating and rearranging lists through some for iterations and append functions, is there a change of the “type” o element doind that? I mean, is the “list” concept changing from a Dynamo list to a Python List?

It’s weird, I copy/pasted your code without changing anything and it works for me. I really don’t know what’s causing the problem here.

1 Like

@T_Pover Thanks for your time!

Does anyone know what could be causing the error?

Now, I tried to change the algorithm, so:

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]:
                inicio[i].append(ind_pisos[k])
                inicio[i].sort()
                fin[i].append(ind_pisos[k])
                fin[i].sort()
            else:
                pass

OUT = inicio

And I got the same error:

image

So, my guess is that this nested list is not being recognized as a list. What can be wrong here?

Could you try inicio[i].Add() instead of append?

It works with Add(), but when I set the sort() method, it shows the same error.

(the number added is at the end of the list)

It seems to be operating on a generic collection’s List instead of a python list… Which is werd because you haven’t imported the Generic Collections

1 Like

Found it!

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

OUT = inicio

I turned it into a list befor getting into a insert().

Thanks for your time!