map is useless!

Terry Reedy tjreedy at udel.edu
Sun Jun 6 17:43:35 EDT 2010


On 6/6/2010 11:16 AM, rantingrick wrote:
> Everyone knows i'm a Python fanboy so nobody can call me a troll for
> this...

Non sequitor. It depends on your intention in posting this...

> Python map is just completely useless. For one it so damn slow

Posting invalid speed comparisons stacked against the feature you are 
dissing is either trollish or lame.

 > why even bother putting it in the language?

Map was put into the language about a decade before comprehensions and, 
I believe, zip. It encapsulates a basic functional programming idiom.

Consider the following snippet: (in 2.x, delete 'list(' and ...')'):

from operator import add
l1 = range(10)
l2 = range(15,30)
print(list(map(add, l1, l2)))
# [15, 17, 19, 21, 23, 25, 27, 29, 31, 33]

Now replace map with a for loop, no zip or listcomp or genexp allowed.
Time how long it takes. Do you get it right the first time, as I did 
with the above?. Your replacememt may or may not *run* faster, but even 
if so, it will hardly be enough to make much different in most uses.

> Maybe GVR should have taken it out in 3.0?

That may have been considered, but map is shorter than the alternative, 
some prefer it stylistically, it can be passed as a function argument 
(including to functool.partial), and its removal would have broken code 
without much gain. It is also handy for explaing generator expressions 
and comprehensions.

Terry Jan Reedy







More information about the Python-list mailing list