lint warnings

Discussion in 'Python' started by Gerald Britton, Feb 15, 2011.

  1. >> I find:
    >>
    >> map(func, iterable)
    >>
    >> to be "neater" than:
    >>
    >> [func(item) for item in iterable]
    >>
    >> If nothing else, the "map" version is shorter.


    >That's only true if you wanted to call an existing function. If you wanted
    >to do something involving a more complex expression that you can write
    >inline then the list comprehension is shorter.


    not necessarily, no.

    >>> [-i if i < 0 else i for i in range(-10,0)]

    [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

    vs.

    >>> map(abs, range(-10,0))

    [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]


    <snip>
    > Also, as already shown, the map version is faster.


    >In most cases the list comprehension is faster. Try timing it.


    I have as have many others (including the previous poster who provided timings)

    >C:\Python27>python.exe lib\timeit.py -s "def double(x): return x*2" -s "data=range(10000)" "map(double, data)"
    >1000 loops, best of 3: 1.82 msec per loop


    C:\Python27>python.exe lib\timeit.py -s "def double(x): return x*2" -s
    "data=range(10000)" "[x*2 for x in data]"
    1000 loops, best of 3: 879 usec per loop

    granted, but not on topic here. we're talking about map vs list comps
    when you want to use a function.

    >map is only likely to be faster if you wanted to call a function in both cases.


    Which is exactly the point.

    >f you have an expression that can be inlined you save the function call
    >overhead with the list comprehension.


    Of course, but that's not the point.


    --
    Gerald Britton
    Gerald Britton, Feb 15, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrea Crotti

    lint warnings

    Andrea Crotti, Feb 14, 2011, in forum: Python
    Replies:
    2
    Views:
    369
    Andrea Crotti
    Feb 15, 2011
  2. Gerald Britton

    lint warnings

    Gerald Britton, Feb 15, 2011, in forum: Python
    Replies:
    1
    Views:
    224
    Steven D'Aprano
    Feb 15, 2011
  3. Gerald Britton
    Replies:
    1
    Views:
    370
    Steven D'Aprano
    Feb 16, 2011
  4. David Sudolcan
    Replies:
    7
    Views:
    2,049
    Eric Sosman
    Apr 4, 2011
  5. Ted Sung
    Replies:
    1
    Views:
    307
    Sherm Pendley
    Aug 30, 2004
Loading...

Share This Page