Do python list comprehensions get converted to pure C?
Question: I've been told multiple times that Python list comprehensions are better than
nested `for`, `if` simply because they are converted to pure C and compiled.
However, I cannot find any documentation to support this; is this true?

For example; the following below code actually does result in 1/3 performance
gain (on my machine):

    
    
    import time
    
    start = time.time()
    a = []
    for i in range(0, 100000000):
      a.append(i)
    
    end = time.time()
    
    print("Time it took: " + str((end - start)))
    
    start = time.time()
    b = [i for i in range(0, 100000000)]
    end = time.time()
    
    print("Time it took: " + str((end - start)))
    

CPython results:

    
    
    Time it took: 12.077988863
    Time it took: 8.65817594528
    

PyPy results:

    
    
    Time it took: 4.9356508255
    Time it took: 0.686870098114
    


Answer: This is implementation specific, but in CPython, it's not compiled to C, but
it is compiled to what is called "bytecode".

We can inspect it with the `dis` module which is used to disassemble bytecode.

    
    
    >>> import dis
    >>> def foo():
    ...     return [i//2 for i in range(20)]
    ... 
    >>> dis.dis(foo)
      2           0 BUILD_LIST               0
                  3 LOAD_GLOBAL              0 (range)
                  6 LOAD_CONST               1 (20)
                  9 CALL_FUNCTION            1
                 12 GET_ITER            
            >>   13 FOR_ITER                16 (to 32)
                 16 STORE_FAST               0 (i)
                 19 LOAD_FAST                0 (i)
                 22 LOAD_CONST               2 (2)
                 25 BINARY_FLOOR_DIVIDE 
                 26 LIST_APPEND              2
                 29 JUMP_ABSOLUTE           13
            >>   32 RETURN_VALUE        
    

In other implementations, it could be compiled to C. I am much less familiar
with those other implementations, so I'll withhold commentary on them.

Regarding your statement:

> list comprehensions are better than nested for, if ...

In some cases, it makes more sense to use the for list and conditions. The
best approach is to use what it most readable to you, and then profile your
code if you need to improve performance. Sometimes you don't actually need the
list or generator created by the list comprehension or generator expression,
and when that's the case, using the for loop with the if condition is
generally more performant.

### speculation:

However, in the example you give, you specifically are intending to create a
list. Appending to the list in a loop is the canonical way to do it, but as
you know from checking your runtimes, the list comprehension is faster. The
reason it is faster is because the designers of Python know a list
comprehension will be populating a list, so there are opportunities to
optimize it. In a for loop, optimizing it would be more difficult, because you
have to be able to process much more complex statements there.

I imagine your huge improvement in PyPy is due to an optimization that
optimizes on the i for i part, and perhaps simply calls `list` on your
iterator.

