Lesson 10
I am Your Father
Chapter 6: Higher order functions
Summary
In this chapter, you looked at two higher-order functions map()
and filter()
.
You have also compared them with using list comprehension equivalents to solving the same problem.
The creator of Python (Guido van Rossum) himself does not really like map()
and filter()
(and really hates another one called reduce()
). He even almost removed them from Python 3. You can read about his opinion in his article back in 2005 if you are interested.
As mentioned earlier, the Python community generally prefers list comprehension as it finds kust comprehension more readable and self-explanatory, thus more friendly to those who are not familiar with functional programming.
As you may have observed, the functions map()
and filter()
(along with lambda functions) can sometimes make your code look cryptic. At the same time, they can sometimes make your code more compact. So you decide what works best for your situation (while keeping ‘readability’ in mind!)