Why does “not(True) in [False, True]” return False?

Operator precedence 2.x, 3.x. The precedence of not is lower than that of in. So it is equivalent to:

>>> not ((True) in [False, True])
False

This is what you want:

>>> (not True) in [False, True]
True

As @Ben points out: It’s recommended to never write not(True), prefer not True. The former makes it look like a function call, while not is an operator, not a function.

Leave a Comment