0
>>> a = [1, 2, 3]
>>> 4 not in a
True
>>> not 4 in a
True

Is there any foolproof way to remember 1. when to use which and 2. which is more prone to errors?

Sunil Kundal
  • 143
  • 1
  • 2
  • 8
  • I believe the first one is considered to be more "pythonic", because it reads well. – Rusty Nov 19 '14 at 09:51
  • always use the first one. they are equivalent, and the second one is a pylint violation. – wim Nov 19 '14 at 09:52
  • related: https://hg.python.org/cpython/file/ed8b0ee1c531/Python/peephole.c#l405 – wim Nov 19 '14 at 09:54
  • 1
    Also, the second one could be mistaken for `(not x) in y`, whereas it is `not(x in y)` – tobias_k Nov 19 '14 at 10:01
  • Yes @tobias_k I guess this ambiguity prevails but must be situations where it is sufficiently necessary to use the `not 4 in a` version. – Sunil Kundal Nov 20 '14 at 12:06

0 Answers0