[BangPypers] nested list question

Jeffrey Jose jeffjosejeff at gmail.com
Wed Jul 14 15:58:15 CEST 2010


> List comprehensions are preferred to map and filter functions.In fact,
> filter is a syntactic sugar to list comprehension.
>
>

You're right in saying list comprehensions are preferred to map and filter -
not just from a readability standpoint but also from a performance
standpoint. (we all hate that .0001 of sec right? - no seriously)

However filter is *not* a syntactic sugar of list comprehension.

>>> l = [1,2,3,4,5,6,7,8]
>>> def foo(a):
...     return [x for x in a if x % 2]
...
>>> def bar(a):
...     return filter(lambda x: x%2, a)
...
>>> foo(l)
[1, 3, 5, 7]
>>> bar(l)
[1, 3, 5, 7]

>>> dis.dis(foo)
  2           0 BUILD_LIST               0
              3 DUP_TOP
              4 LOAD_ATTR                0 (append)
              7 STORE_FAST               1 (_[1])
             10 LOAD_FAST                0 (a)
             13 GET_ITER
        >>   14 FOR_ITER                31 (to 48)
             17 STORE_FAST               2 (x)
             20 LOAD_FAST                2 (x)
             23 LOAD_CONST               1 (2)
             26 BINARY_MODULO
             27 JUMP_IF_FALSE           14 (to 44)
             30 POP_TOP
             31 LOAD_FAST                1 (_[1])
             34 LOAD_FAST                2 (x)
             37 CALL_FUNCTION            1
             40 POP_TOP
             41 JUMP_ABSOLUTE           14
        >>   44 POP_TOP
             45 JUMP_ABSOLUTE           14
        >>   48 DELETE_FAST              1 (_[1])
             51 RETURN_VALUE
             52 LOAD_CONST               0 (None)
             55 RETURN_VALUE

>>> dis.dis(bar)
  2           0 LOAD_GLOBAL              0 (filter)
              3 LOAD_CONST               1 (<code object <lambda> at
0x2a955e4d50, file "<stdin>", line 2>)
              6 MAKE_FUNCTION            0
              9 LOAD_FAST                0 (a)
             12 CALL_FUNCTION            2
             15 RETURN_VALUE
             16 LOAD_CONST               0 (None)
             19 RETURN_VALUE


The alleged performance gain of list comprehension comes from the fact that
`filter` involve a function call (here= lambda). I'm sure this can be easily
proved using python -m timeit

Then what is syntatic sugar ?
This.

>>> def foo(a):
...    a += 1
...    return a
...
>>> def bar(a):
...    a = a+1
...    return a
...
>>> foo(10)
11
>>> bar(10)
11
>>> dis.dis(foo)
  2           0 LOAD_FAST                0 (a)
              3 LOAD_CONST               1 (1)
              6 INPLACE_ADD
              7 STORE_FAST               0 (a)

  3          10 LOAD_FAST                0 (a)
             13 RETURN_VALUE
             14 LOAD_CONST               0 (None)
             17 RETURN_VALUE
>>> dis.dis(bar)
  2           0 LOAD_FAST                0 (a)
              3 LOAD_CONST               1 (1)
              6 BINARY_ADD
              7 STORE_FAST               0 (a)

  3          10 LOAD_FAST                0 (a)
             13 RETURN_VALUE
             14 LOAD_CONST               0 (None)
             17 RETURN_VALUE



/jeff


More information about the BangPypers mailing list