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."
Python is the main dynamic language used at Google. This style guide is a list of dos and don'ts for Python programs. To help you format code correctly, we've created a settings file for Vim. For Emacs, the default settings should be fine. Many teams use the yapf auto-formatter to avoid arguing over formatting.
PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.
This style guide provides editorial guidelines for writing clear and consistent Google-related developer documentation. If you're new to the guide and looking for introductory topics about our style, then start with Highlights, Voice and tone, and Text-formatting summary.
Programming style, also known as code style, is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers read and understand source code conforming to the style, and help to avoid introducing errors.
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.
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 [211]: %timeit list(map(str,range(100)))
7.81 µs ± 151 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [215]: %timeit [str(item) for item in range(100)]
10.3 µs ± 3.06 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With