x = [1,2,3]
y = [1]
z = [0, 1, False]
if 1 in (x, y, z):
print('passed')
Why does this code not print passed? Since 1 is in each of them shouldn't it print passed? When I put only one of the variables it does print passed though.
Your code is checking whether 1 is equal to x or y or z (which it is not, since they are all lists and 1 is a number). What you want to do instead is check if 1 is in all of the lists:
x = [1,2,3]
y = [1]
z = [0, 1, False]
if all(1 in l for l in (x, y, z)):
print('passed')
Output:
passed
In your code,
if 1 in (x, y, z):
checks 1 in ([1, 2, 3], [1], [0,1,False]) In the condition its like ( [list1], [list2], [list3] ) So its checking, is list1, list2 or list3 is equal to 1 or not!
So the full condition is look like this:
[1, 2, 3] = 1 # False
[1] = 1 # False
[0, 1, False] = 1 #False
As you can see everything is false, during the if statement.
Use this simple syntex instead :
if (1 in x) or (1 in y) or (1 in z):
print ("passed...")
in (x, y, z)means.xcontains three ints, one of which is 1, so1 in xis true.(x,y,z)contains three lists, not ints, so1 in (x,y,z)is false.(x,y,z)it checks each list but for the y variable even though it has 1, it is seen as[1]which is not exactly1.. But it does pass the flag if only one variable is listed at a time because it checks each item in the list? Thank you!