Filter a list with two conditions

Hi there,

I’m working on a script to filter a list of values with two conditions, but it doesn’t work. The list has values equal to 1 and -1, the idea is a script that returns True if the value is equal to them and false if it is different.

Can someone help me to find what is the error?

List = [0]
result = [] 

for i in List:
	if i == 1:
	 result.append(True)
	elif i ==-1:
	 result.append(True)
	else:
	 result.append(False)

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


script 2

@Joze Change the 1st line from

List = [0]

to

List =IN[0]

Not sure why you chose the Python route but using a code block would be much simpler!


4 Likes

I’m learning python therefore I tried to use it.

Thank you for your reply. :slightly_smiling_face:

1 Like

Hello @AmolShah

Can you please show me your code inside the python script?

I added IN where you indicated but my code didn’t work. The other options work great!!

You need to use a tab after you’re if and else statement not a space.

I’ll try that. Thank you.

@Joze Try this:

List = IN[0]
result = [] 

for i in List:
	if i == 1:result.append(True)
	elif i ==-1:result.append(True)
	else:result.append(False)

OUT = result

Or using a tab instead of space as suggested (although it worked for me with the spaces)

Thank you :grin: