# Python: Loop through list and IF statement

**URL:** <https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577>\
**Category:** Lists-Logic\
**Tags:** loop, python\
**Created:** [August 16, 2019, 9:43pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577 "2019-08-16T21:43:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![spenny18](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@spenny18](https://forum.dynamobim.com/u/spenny18)\
**Post date:** [August 16, 2019, 9:43pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/1 "2019-08-16T21:43:29Z")

</div>

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

```

 ![Capture](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/5/9594d1391f370a47e08968d013c5632ddc54dfcc.png)[numbers.txt](https://forum.dynamobim.com/uploads/short-url/lcEj7LPe0NvCnhqSy0TJj7Y6LLL.txt) (587 Bytes)

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [August 16, 2019, 10:10pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/2 "2019-08-16T22:10:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![spenny18](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@spenny18](https://forum.dynamobim.com/u/spenny18)\
**Post date:** [August 16, 2019, 10:23pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/3 "2019-08-16T22:23:57Z")

</div>

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/](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
```

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [August 16, 2019, 10:28pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/4 "2019-08-16T22:28:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thomas\_Corrie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thomas_corrie/32/63060_2.png) [@Thomas\_Corrie](https://forum.dynamobim.com/u/Thomas_Corrie)\
**Post date:** [August 16, 2019, 10:53pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/5 "2019-08-16T22:53:31Z")

</div>

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]]`

---

<div class="post-metadata">

**Author:** ![ingenieroahmad](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ingenieroahmad/32/72992_2.png) [@ingenieroahmad](https://forum.dynamobim.com/u/ingenieroahmad)\
**Post date:** [August 18, 2019, 7:19am UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/6 "2019-08-18T07:19:00Z")

</div>

Here is another simple way away of python.

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/6/f/6f06d312eed7df2c7fcee67c23b57989f5e3ad26.png)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [August 18, 2019, 2:54pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/7 "2019-08-18T14:54:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [April 7, 2021, 5:33pm UTC](https://forum.dynamobim.com/t/python-loop-through-list-and-if-statement/39577/8 "2021-04-07T17:33:08Z")

</div>

Split this topic on April 7, 2021, 5:33pm UTC

2 posts were split to a new topic: [If statement with input method (Python)](https://forum.dynamobim.com/t/if-statement-with-input-method-python/62634)
