Python Programming to Exclude Numbers in List

I have a code block that creates a list of 60 numbers. I am trying to remove numbers in the list if they are less than 2.1. To do this I have written this code:
result = []
for item in area:
if item < 2.1:
result.append(item)
area = result
The code block appears red and says there is an error. Is this a problem with my syntax or am I missing an easier way to do this?

in Python you can do this:

#Assign your output to the OUT variable.
OUT = [i for i in IN[0] if i < 2.1]
1 Like

Thank you, How do I go about assigning OUT to the output? Sorry I am new to dynamo

1 Like

This is what I have here, am I doing it right? Sorry to be a pain

It needs to be in a python script not a code block as shown by Konrad.

Thank you so much for your help, it worked!

Can this code be easily changed to instead of removing the number to changing it to something else? Just asking for future reference

OUT = [i if i < 2.1 else "someting else" for i in IN[0]]

Thank you for that, I’m sure I’ll use it again

Know it’s solved but for future reference you can do it in a code block with design script as well:

4 Likes

Ah! Indeed. It even looks like a more elegant solution if you ask me. I went off the assumption that user posted a sample of Python code, but your solution looks better to me.