Map vs. List Comprehensions (was "lint warnings")
steve+comp.lang.python at pearwood.info
Wed Feb 16 14:35:24 CET 2011
On Tue, 15 Feb 2011 18:55:50 -0500, Gerald Britton wrote:
> So, what's the feeling out there? Go with map and the operators or
> stick with the list comps?
Stick to whatever feels and reads better at the time.
Unless you have profiled your code, and determined that the map or list
comp was the bottleneck, who cares if you save half a nanosecond per
map clearly loses badly in the old-time idiom:
map(lambda x: x+1, seq)
or similar. That's the only time I'd just flat out say, avoid map in
favour of a list comprehension. Otherwise, the performance difference is
likely to be trivial, as is the difference in length of code. Use
whichever you like.
I personally appreciate the ability to treat map as a primitive ("map
this function over this data") rather than caring about the mechanics of
iteration ("iterate over data, applying this function to each element in
turn"), so I often use map. But I use list comps even more often.
More information about the Python-list