Because that's not what any
does. any
takes an iterable of values, interprets them as booleans, and returns a boolean indicating whether any of them were True
. At least one of [20.0, 0.0, 19.0, 1.0]
is nonzero, which means it counts as True
, so any([20.0, 0.0, 19.0, 1.0])
evaluates to True
, and your print statement is equivalent to print True in [20.0, 0.0]
. Which is itself False
.
To do this with any
, use a generator expression:
print any(x in [20.0, 0.0] for x in [20.0, 0.0, 19.0, 1.0])
If you're doing this for any significant number of values, you'll get major performance improvements from using a set. in
on a list is linear with the length of the list, while in
on a set is constant time.
targets_set = set([20.0, 0.0])
print any(x in targets_set for x in [20.0, 0.0, 19.0, 1.0])