+ 4
Python: ‘a’ in arr in arr - Why False?
arr = [1, True, 'a', 2] print('a' in arr in arr) # False This was a SoloLearn match and I would like to understand how it is exactly interpreted by Python. Certainly not this way: print(('a' in arr) in arr) because it would be equivalent to 1 in arr which is clearly True. I can also not translate it to print('a' in (arr in arr)) because I get a TypeError: argument of type 'bool' is not iterable. Is the first code refer to an embedded list? If yes, where is the embedded list exactly?
5 Antworten
+ 4
It's because of operator chaining.
Similar to how you can write in python:
0 < x < 10
https://www.tutorialspoint.com/chaining-comparison-operators-in-python
+ 2
Tibor Santa Thanks Tibor!
“if the expression is x < y < z, then it is similar to the x < y and y < z”
+ 1
Mirielle👽 Seems to solve the problem. Could you formulate a general rule for that?
0
Jay Matthews See Mirielle’s answer
- 2
Suraj parida