Yesterday, I got a great question from one of my “Coffee Break Python” email course readers:
I once read that {list, dictionary, tuple} comprehension can substitute any lambda code. Is that true? Is lambda
, filter
, map
really essential? — Albrecht, Finxter Member
Python’s creator, Guido van Rossum, doesn’t like functional programming a lot. That’s well-known.
Guido: “I value readability and usefulness for real code. There are some places where map()
and filter()
make sense, and for other places Python has list comprehensions. I ended up hating reduce()
because it was almost exclusively used (a) to implement sum()
, or (b) to write unreadable code. So we added built-in sum()
at the same time we demoted reduce()
from a built-in to something in functools
(which is a dumping ground for stuff I don’t really care about :-).”
This is what Guido meant when arguing that the reduce()
function is mainly used as a replacement for sum()
:
from functools import reduce print(reduce(lambda x, y: x + y, range(1, 6))) # 15
The reduce function takes two arguments: a function that takes two arguments and returns one value, and an iterable. It then repeatedly applies the function to two values from the iterable until only one value is left. Feel free to study my blog tutorial about the reduce function (5-min read).
Although the function seems to be useful, most people don’t find compelling use cases in practice. They use the function to calculate the sum of all values in an iterable—but that’s it.
However, the same objective can easily be achieved with the built-in functions sum()
:
print(sum(range(1,6))) # 15
That’s why Guido initiated to remove reduce
from Python 3 (in Python 2, the reduce function is a built-in function). Here’s the famous article where Guido describes his arguments against reduce
(and lambda
, map()
, and filter()
). Study this article thoroughly, every Python expert knows about the arguments in the article)!
Guidos Arguments Against lambda
, map()
, filter()
, and reduce()
Guido has a few specific arguments against including those functional programming elements in the Python language:
[Argument 1] Filtering is better done with list comprehension.
Here’s an example of the filter function:
customers = ["Alice", "Bob", "Frank", "Ann"] a = filter(lambda x: x[0] == "A", customers) print(list(a)) # ['Alice', 'Ann']
We get rid of all customers who do not start with the character "A"
. However, this can easily be achieved with the following statement:
a = [ x for x in customers if x[0] == "A" ] print(a) # ['Alice', 'Ann']
Besides being slightly shorter, the list comprehension statement is also faster and don’t require to define a separate lambda
function.
Note that if you have to refresh your list comprehension skills, feel free to read about this comprehensive tutorial on my blog.
[Argument 2] The map()
function can also be replaced by list comprehension
Here’s an example of the map()
function:
customers = ["Alice", "Bob", "Frank", "Ann"] lst1 = map(lambda x: x[0], customers) print(list(lst1)) # ['A', 'B', 'F', 'A']
The first argument of the map() function is a (lambda) function and the second argument is an iterable. The first argument defines how each element from the iterable should be transformed (mapped).
However, this can better be achieved with list comprehension (and it’s also more efficient):
lst2 = [ x[0] for x in customers ] print(lst2) # ['A', 'B', 'F', 'A']
This solution is more readable, needs a lower number of characters, and is more efficient. It’s a no-brainer that list comprehension is the way to go here.
[Argument 3] With filter()
, map()
, and reduce()
gone, the main argument of keeping lambda
around is gone, too.
The lambda
function is used sometimes to specify callback functions (e.g. for Tkinter user interfaces). However, in those cases, you can (and most often: should) define the callback function explicitly.
Why You Should Still Study Those Functions
But this does not change the fact that you should know these functions!
The lambda function is useful (although not required — you could define every function using the def
keyword although this would reduce readability in many cases IMO).
Many people keep using them—especially the ones who come from functional programming. You could argue against many words in the English language but as long as people are using them, it doesn’t help. You need to learn those words to be able to communicate well.