0
print(1 in [1] == True)  # False Why??????????????????
print((1 in [1]) == True)  # True OK
print(1 in ([1] == True))  # TypeError: argument of type 'bool' is not iterable OK

So just what is happening here? How Python interprets this statement?

Anton Ovsyannikov
  • 1,010
  • 1
  • 12
  • 30
  • 4
    Your first line is an example of a *chained comparison* - it's equivalent to `(1 in [1]) and ([1] == True)` (except that the shared part of the two sub-expressions, `[1]` in this case, is only evaluated once). This is more commonly used in forms like `0 <= x < 100`, but it works with any comparison operators - and `in` is one of them. – jasonharper Feb 13 '23 at 20:30
  • Could you please write an answer (not comment) to accept? – Anton Ovsyannikov Feb 13 '23 at 20:32
  • 3
    It's a duplicate, see e.g. https://stackoverflow.com/q/40987185/3001761, https://stackoverflow.com/q/6074018/3001761 – jonrsharpe Feb 13 '23 at 20:34
  • Actually comparison with exactly True or False is quite confusing in this case, so not sure it's a full dublicate. – Anton Ovsyannikov Feb 13 '23 at 20:38

0 Answers0