2

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
Patrick Perini
  • 22,361
  • 12
  • 57
  • 88
stenci
  • 7,719
  • 12
  • 58
  • 95

2 Answers2

1

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.

AlexQueue
  • 6,138
  • 5
  • 29
  • 43
1

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.

Whatang
  • 8,682
  • 1
  • 20
  • 24