+ 2
How nested "in" evaluated in python
l = [1, True, 'a'] print(1 in l in l) # outputs False print((1 in l) in l) # outputs True Why it gives False for first statment even though they are equivalent
2 Antworten
+ 7
They are not equivalent, and this is not nesting.
It is called "operator chaining".
x in y in z
is the same as:
(x in y) and (y in z)
similar to: x < y < z
https://www.geeksforgeeks.org/chaining-comparison-operators-JUMP_LINK__&&__python__&&__JUMP_LINK/
Unrelated note: never use lowercase L or uppercase i as variable name, it is too confusing even in monospace font.
+ 1
Thank you sir