+ 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

1st May 2024, 3:37 PM
Anshika
Anshika - avatar
2 odpowiedzi
+ 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.
1st May 2024, 4:49 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you sir
1st May 2024, 4:51 PM
Anshika
Anshika - avatar