The two forms value not in list
and not value in list
return the same result.
Are they equivalent or is one better than the other?
>>> l=[1,2,3]
>>> 1 not in l
False
>>> not 1 in l
False
>>> 5 not in l
True
>>> not 5 in l
True
The two forms value not in list
and not value in list
return the same result.
Are they equivalent or is one better than the other?
>>> l=[1,2,3]
>>> 1 not in l
False
>>> not 1 in l
False
>>> 5 not in l
True
>>> not 5 in l
True
I would say value not in list
is better simply because of readability. not value in list
is confusing. Code should be as readable as possible.
From http://docs.python.org/2/reference/expressions.html#not-in:
x in s evaluates to true if x is a member of the collection s, and false otherwise. x not in s returns the negation of x in s
So, x not in s
is indeed the same as not x in s
. Whether one is better than the other is a matter of style; personally, I'd say that x not in s
reads better.