Inserting element into list with python

#1

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!

0 Likes

#2

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?

0 Likes

#3

0 Likes

Python Nodes Basics
#4

1 Like

#5

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?

0 Likes

#6

and this is fin:

0 Likes

#7

Seems to work for me:

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

0 Likes

#8

What do you mean?

0 Likes

#9

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.

0 Likes

#10

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

0 Likes

#11

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
0 Likes

#12

@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?

0 Likes

#13

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

#14

@T_Pover Thanks for your time!

Does anyone know what could be causing the error?

0 Likes

#15

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?

0 Likes

#16

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

0 Likes

#17

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

0 Likes

#18

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

0 Likes

#19

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

#20

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!

0 Likes