List.index(item): item not in list

Hi,

Pretty simple stuff here but cant get it to work.

I have a list ‘list1’ containing data as such: [ [element1, [x1, y1]] , [element2, [x2, y2]], etc ]
Then there is a list ‘firstitemselected’ with 1 item [ element1, [x1, y1] ]

I use these lists in a Python Script.
I want to find the index of ‘firstelementselected’ in ‘list1’.

I use the code:

list1 = IN[0]
firstitemselected = IN[1]

OUT = list1.index(firstitemselected)

Somehow i get returned: ’ item not in list’ . But a visual check ensures that the item is definitely in the list. See for yourself:

Whats going wrong here?

Thanks in advance.

Did you mean to put firstitem in list1.index(first), instead of just first

sorry i messed up. code is like this:

2018-10-19%20(2)

Try flattening the firstitemselected by one level:

Python:

# Enable Python support and load DesignScript library
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

mainL = IN[0]
checkFor = IN[1]

OUT = mainL.index(checkFor)
1 Like

I do not believe you can compare entire list structures like that… As you’re not comparing the “A01-C…” but rather the entire structure [“A”[80,71]]…

Also outputting in python would read “OUT=…” not print.

Doing something like this could get you the index?

1 Like

Try flattening the firstitemselected by one level

That’s better. Thank you.
However, problem not completely solved yet. (see next post)

1 Like

There is still something happening that I do not understand.

Method A = working

firstitemselected = IN[0]

list1 = IN[1]

firstitemindex = list1.index(firstitemselected)
firstitem = list1[firstitemindex]
list2 = [firstitem]

OUT = list2

List 1 is defined in another python code block as follows

elements = IN[0]
coords = IN[1]   

list1 = list(zip(elements,coords))

OUT = list1

Method B = Not Working

I am trying to merge the two code blocks as such:

firstitemselected = IN[0]

elements = IN[1]
coords = IN[2]   

list1 = list(zip(elements,coords))

firstitemindex = list1.index(firstitemselected)
firstitem = list1[firstitemindex]
list2 = [firstitem]

OUT = list2

And now i suddenly get " item not in list " .

What is the difference ???

You’re just not wiring correctly your second Python node :slight_smile:

Doh! Thats pretty stupid… good catch

however… after rewiring also still the same error…

Might @Jonathan.Olesen be right in saying that you cannot compare entire list structures like this ?

Try outputting at several moment of the not working python script to see if it is doing what you want it to be doing (and eventually compare it to your unmerged scripts)

Can you upload a graph to test on / with an example?

@mellouze

I do not believe you can compare entire list structures like that… As you’re not comparing the “A01-C…” but rather the entire structure [“A”[80,71]]…

@Jonathan.Olesen This shows me that it does work:

Yea you’re right… Cannot see why it will not work in a continued script though…

Cannot see why it will not work in a continued script though…

Me neither!

If anyone has an idea why, please share!

Okay, this will be my workaround for the moment.

But still, if anyone can help getting behind “the python reason” why previous attempts failed, please help.