Credit: Luther Blissett
You want to perform an operation on
all the elements of a list, but you’d like to avoid
using map
and filter
because
they can be hard to read and understand, particularly when they need
lambda
.
Say you want to create a new list by adding 23 to each item of some other list. In Python 1.5.2, the solution is:
thenewlist = map(lambda x: x + 23, theoldlist)
This is hardly the clearest code. Fortunately, since Python 2.0, we can use a list comprehension instead:
thenewlist = [x + 23 for x in theoldlist]
This is much clearer and more elegant.
Similarly, say you want the new list to comprise all items in the other list that are larger than 5. In Python 1.5.2, the solution is:
thenewlist = filter(lambda x: x > 5, theoldlist)
But in modern Python, we can use the following list comprehension:
thenewlist = [x for x in theoldlist if x > 5]
Now say you want to combine both list operations. In Python 1.5.2, the solution is quite complex:
thenewlist = map(lambda x: x+23, filter(lambda x: x>5, theoldlist))
A list comprehension affords far greater clarity, as we can both
perform selection with the if
clause and use some
expression, such as adding 23, on the selected items:
thenewlist = [x + 23 for x in theoldlist if x > 5]
Elegance and clarity, within a generally pragmatic attitude, are
Python’s core values. List comprehensions, added in
Python 2.0, delightfully display how pragmatism can enhance both
clarity and elegance. The built-in map
and
filter
functions still have their uses, since
they’re arguably of equal elegance and clarity as
list comprehensions when the lambda
construct is
not necessary. In fact, when their first argument is another built-in
function (i.e., when lambda
is not involved and
there is no need to write a function just for the purpose of using it
within a map
or filter
), they
can be even faster than list comprehensions.
All in all, Python programs optimally written for 2.0 or later use
far fewer map
and filter
calls
than similar programs written for 1.5.2. Most of the
map
and filter
calls (and quite
a few explicit loops) are replaced with list comprehensions (which
Python borrowed, after some prettying of the syntax, from Haskell,
described at http://www.haskell.org).
It’s not an issue of wanting to play with a shiny
new toy (although that desire, too, has its place in a
programmer’s heart)—the point is that the toy,
when used well, is a wonderfully useful instrument, further enhancing
your Python programs’ clarity, simplicity, and
elegance.
Get Python Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.