Python: Loop through list and IF statement

Hi all,

I am having some trouble with a basic python function, and am need of help.

Basically, I am working on a furniture plan, and just learned that some cubicle panels only come in certain lengths. I want to check to make sure that our plans only show the correct lengths and the incorrect ones are highlighted.

In dynamo, I have collected all of the cubicles and their widths. I want to check if any item in the list is equal to 54" (4.5’) OR 66" (5.5’). If it is, I want to be able to do Boolean Mask filter to separate the types.

My issue is that I am checking a list and it is returning a single item. I want it to replace the 54 or 66 values with “invalid panel” and the ones that don’t equal those items to be “valid panels.”

Here is my python code, and an export of my workspace, along with a text file of the panel widths.

Any help is appreciated!!!

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.
dataEnteringNode = IN
w = IN[0]

#condition
for i in w:
	if w == 4.5 or w == 5.5:
		w = "invalid panel"
	else:
		w = "valid panel"

#Assign your output to the OUT variable.
OUT = w

numbers.txt (587 Bytes)

There’s a few things wrong with this…

    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.
    dataEnteringNode = IN

    w = IN[0]
    arr []

    #condition
    for i in w:
    	if i == 4.5 or i == 5.5:
    		arr.append(False) 
    	else:
    		arr.append(True) 

    #Assign your output to the OUT variable.
    OUT = arr

Firstly if w is a list of numbers (widths) then you are asking if that list is equal to 4.5 or 5.5. Instead you want to be asking if i == 4.5 or 5.5.

Secondly I would put arr = [] above the for loop and instead of w = "invalid panel" put arr.append(False) and similarly for w = "valid panel" put arr.append(True) and then finally, OUT = arr

The reason you are getting only one output is because you are only assigning to w, as it loops through w, you are assigning w each time and leaving only one item as w when the loop ends.

Hope that makes sense…

Cheers,
Dan

3 Likes

Thank you for the reply! That makes sense (asking each item in the list, rather than asking the list). This helped me out.

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.
dataEnteringNode = IN

w = IN[0]
arr = []

#condition
for i in w:
	if i == 4.5 or i == 5.5:
		arr.append(False) 
	else:
		arr.append(True) 

#Assign your output to the OUT variable.
OUT = arr

Also, after some googling, I found this (https://www.saltycrane.com/blog/2008/08/how-conditionally-replace-items-list/) which also helped, using the enumerate method:

for (i, x) in enumerate(w):
	if x == 4.5 or x == 5.5:
		w[i] = "invalid"
	else:
		w[i] = "valid"

#Assign your output to the OUT variable.
OUT = w
3 Likes

That’s great @spenny18, the enumerate method is also very useful in these cases. You pretty much solved it yourself! :wink:

1 Like

Also possible to condense it down to a single line using list comprehension:

OUT = [False if x == 4.5 or x == 5.5 else True for x in IN[0]]

5 Likes

Here is another simple way away of python.

image

4 Likes

You could also swap those trues and glades for an workstation and a null, and wrap the whole thing in a List.Clean function to just get the objects (workstations) you are after.

1 Like

2 posts were split to a new topic: If statement with input method (Python)