Why does the Google Python Style Guide prefer list comprehensions and for loops instead of filter, map, and reduce?

Deprecated Language Features: ... "Use list comprehensions and for loops instead of filter, map, and reduce. "

The explanation given : "We do not use any Python version which does not support these features, so there is no reason not to use the new styles."

share|improve this question
up vote 21 down vote accepted

map and filter are way less powerful than their list comprehension equivalent. LCs can do both filtering and mapping in one step, they don't require explicit function and can be compiled more efficiently because of their special syntax

# map and filter
map(lambda x:x+1, filter(lambda x:x%3, range(10)))
# same as LC
[x+1 for x in range(10) if x%3]

There is simply no reason to prefer map or filter over LCs.

reduce is slightly different, because there is no equivalent LC, but it has no big advantage over a ordinary for-loop either.

share|improve this answer
9  
The canonical use case where map() is still arguably better is applying an existing single argument function to a sequence, such as map(str, seq). That kind of operation is the reason it was eventually retained for Python 3. – ncoghlan Mar 25 '11 at 4:26

The Google Python Style guide does not say

prefer list comprehensions and for loops instead of filter, map, and reduce

Rather, the full sentence reads,

Use list comprehensions and for loops instead of filter and map when the function argument would have been an inlined lambda anyway. (my emphasis)

So it is not recommending that you completely avoid using map, for instance -- only that

[expression(item) for item in iterable] 

is preferable to

map(lambda item: expression(item), iterable)

In this case it is clear that the list comprehension is more direct and readable.

On the other hand, there is nothing wrong with using map like this:

map(str, range(100))

instead of the longer-winded

[str(item) for item in range(100)]

It performs well to boot:

In [57]: %timeit map(str,range(100))
100000 loops, best of 3: 12.6 us per loop

In [58]: %timeit [str(item) for item in range(100)]
100000 loops, best of 3: 17 us per loop
share|improve this answer
4  
+1 for putting the quote in context! – wim May 2 '13 at 4:21
1  
+1 for performance comparison... interesting that map runs faster in this case. – noumenon Oct 21 '13 at 8:10

List comprehensions are generally considered more "pythonic" than filter, map and reduce.

See also this article by Python creator Guido van Rossum.

As far as filing this under "Deprecated Language Features" in the style guide, there were apparently plans to deprecate filter, map and reduce in Python 3 (see the article referenced above).

Some of these plans changed eventually. reduce was dropped from being a built-in function (and moved to the functools module), but filter and map are still available as built-ins.

share|improve this answer
2  
functools itself is part of the standard library. The demotion of reduce() was actually just making it no longer a builtin. – ncoghlan Mar 25 '11 at 4:22
    
Thanks ncoghlan. I edited my answer to reflect this. – cschol Mar 25 '11 at 11:41

I would think that it is because not everybody knows how to use those functions well; readability may be impaired for people who are not as familiar. Also, the for loop and list comprehension are widely used and easy to understand; even though the latter is from functional programming, just like map, filter, and reduce, it mirrors lists and for loops quite well. In any case, cramming a lambda or defining a function just to use with map, filter, or reduce can get annoying, especially since a lambda can only be a single expression and a function could clutter your code. You don't need them anyways; map(func, seq) is just [func(x) for x in seq] and filter is just a list comprehension with an if component. reduce can be done with a for loop.

In short, for and list comprehensions are clearer, and they provide basically equivalent functionality in most cases.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.