35

I have seen it both ways, but which way is more Pythonic?

a = [1, 2, 3]

# version 1
if not 4 in a:
    print 'is the not more pythonic?'

# version 2
if 4 not in a:
    print 'this haz more engrish'

Which way would be considered better Python?

Joël
  • 2,723
  • 18
  • 36
ThePrimeagen
  • 4,462
  • 4
  • 31
  • 44

4 Answers4

48

The second option is more Pythonic for two reasons:

  • It is one operator, translating to one bytecode operand. The other line is really not (4 in a); two operators.

    As it happens, Python optimizes the latter case and translates not (x in y) into x not in y anyway, but that is an implementation detail of the CPython compiler.

  • It is close to how you'd use the same logic in the English language.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 6
    Can you site that source? – ThePrimeagen Jul 15 '13 at 16:46
  • 1
    @AshwiniChaudhary: That is a CPython optimisation made by the compiler – Martijn Pieters Jul 15 '13 at 16:48
  • 3
    @MartijnPieters Found it http://hg.python.org/cpython/file/ed8b0ee1c531/Python/peephole.c#l405 ;) – Ashwini Chaudhary Jul 15 '13 at 17:04
  • 1
    @AshwiniChaudhary: exactly. The peephole optimisations is what I was talking about. – Martijn Pieters Jul 15 '13 at 17:08
  • 2
    This remains an opinion, which, fortunately, nobody is obliged to adhere to. – michaelmeyer Jul 15 '13 at 17:33
  • 2
    @doukremt The fact is there are references, pythonic is a thing that can be understood, and there is specific expertise. I find it a bit ironic those who don't understand these things can close something based on their opinion that this is an opinion. It is a fact the second one is more pythonic usage of the language. Let me make this simple `[bar for bar in baz if not not not not not '0' not in str(bar)]` is not pythonic. Not `[bar for bar in baz if not not not not not '0' not in str(bar)]` not is pythonic. – Derek Litz Dec 12 '13 at 01:34
22

Most would agree that 4 not in a is more Pythonic.

Python was designed with the purpose of being easy to understand and intelligible, and 4 not in a sounds more like how you would say it in English - chances are you don't need to know Python to understand what that means!

Note that in terms of bytecode, the two will be identical in CPython (although not in is technically a single operator, not 4 in a is subject to optimization):

>>> import dis
>>> def test1(a, n):
        not n in a


>>> def test2(a, n):
        n not in a


>>> dis.dis(test1)
  2           0 LOAD_FAST                1 (n)
              3 LOAD_FAST                0 (a)
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        
>>> dis.dis(test2)
  2           0 LOAD_FAST                1 (n)
              3 LOAD_FAST                0 (a)
              6 COMPARE_OP               7 (not in)
              9 POP_TOP             
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • That's because the CPython compiler heads this off with a [peephole optimisation](http://hg.python.org/cpython/file/ed8b0ee1c531/Python/peephole.c#l405), but that's not to say that IronPython and Jython do the same thing. – Martijn Pieters Jul 15 '13 at 17:13
  • @MartijnPieters Right, I made a note of that. Do you know for a fact whether the other implementations do or do not (I assume they do)? – arshajii Jul 15 '13 at 17:25
  • I know that the peephole optimisations are not part of the language specification, so other implementations are entirely free to add those or ignore those. – Martijn Pieters Jul 15 '13 at 17:26
  • There is no code that I can see in the [Jython compiler](http://hg.python.org/jython/file/7eb5574d023d/src/org/python/compiler) that makes this same optimisation. – Martijn Pieters Jul 15 '13 at 17:36
  • Good pointing out that 'not in' is its own operator – edaniels Jul 15 '13 at 17:45
8

I believe not in is more widely used.

While the PEP 8 style guide doesn't explicitly discuss the topic, it does consider not in to be its own comparison operator.

Don't forget about The Zen of Python. One of the core tenets of writing Python is that "Readability counts," so go with the choice that is the clearest to read and understand in your code.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
2

Although 4 not in a is to be preferred when the choice is made in isolation, there can be cases when the other choice not 4 in a is to be preferred.

For example, if the spec for the software is written to match not 4 in a then it might be best to leave it matching the spec to aid when checking the software against the spec.

A further example is that one way allows this expression of deteriorating health:

( 4 in well,
  well,
  not well,
  not 4 in well) #!
Paddy3118
  • 4,704
  • 27
  • 38