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,555
  • 12
  • 59
  • 88
stenci
  • 8,290
  • 14
  • 64
  • 104

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,353
  • 5
  • 35
  • 44
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
  • 9,938
  • 2
  • 22
  • 24