Unexpected behavior of operator "in" when checking if a list/tuple/etc. contains a value: >>> 1 in [1] is True False >>> (1 in [1]) is True True Is this a bug? If not, please explain why first variant return False.
Check out this section of the documentation, notably this part: "Note that comparisons, membership tests, and identity tests, all have the same precedence and have a left-to-right chaining feature" Chaining lets you write stuff like this: >>> x = 1 >>> 0 < x < 2 True And since membership tests and identity tests are chained, the code you posted above essentially turns into: (1 in [1]) and ([1] is True) The former part of that expression is True but the latter is false.
I am sure that some time ago I read that `in` is a comparison operator but I forgot it and I was thinking that (x in y) would be equivalent to (replaced with) the return value of y.__contains__(x).