If I do this:
>>> False in [False, True] True
True. Simply because
False is in the list.
But if I do:
>>> not(True) in [False, True] False
not(True) is equal to
>>> not(True) False
>>> 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. The former makes it look like a function call, while
not is an operator, not a function.
not x in y is evaluated as
x not in y
You can see exactly what"s happening by disassembling the code. The first case works as you expect:
>>> x = lambda: False in [False, True] >>> dis.dis(x) 1 0 LOAD_GLOBAL 0 (False) 3 LOAD_GLOBAL 0 (False) 6 LOAD_GLOBAL 1 (True) 9 BUILD_LIST 2 12 COMPARE_OP 6 (in) 15 RETURN_VALUE
The second case, evaluates to
True not in [False, True], which is
>>> x = lambda: not(True) in [False, True] >>> dis.dis(x) 1 0 LOAD_GLOBAL 0 (True) 3 LOAD_GLOBAL 1 (False) 6 LOAD_GLOBAL 0 (True) 9 BUILD_LIST 2 12 COMPARE_OP 7 (not in) 15 RETURN_VALUE >>>
What you wanted to express instead was
(not(True)) in [False, True], which as expected is
True, and you can see why:
>>> x = lambda: (not(True)) in [False, True] >>> dis.dis(x) 1 0 LOAD_GLOBAL 0 (True) 3 UNARY_NOT 4 LOAD_GLOBAL 1 (False) 7 LOAD_GLOBAL 0 (True) 10 BUILD_LIST 2 13 COMPARE_OP 6 (in) 16 RETURN_VALUE
This book project was first presented to me during my first week in my current role of managing the data mining development at SAS. Writ- ing a book has always been a bucket‐list item, and I was ver...
In the summer of 2008, after three decades of building software in corporate data centers, I walked away from corporate America to take a stab at building a technology company from the ground up based...
Efficiently perform data collection, wrangling, analysis, and visualization using Python. Recent advancements in computing and artificial intelligence have completely changed the way we understand ...
R for Everyone: Advanced Analytics and Graphics. ...