Using the higher-order functions

A function which accepts a function as an argument, or returns a function as a result, is called a higher-order function. Python has a number of higher-order functions. The most commonly-used of these functions are map(), filter(), and sorted(). The itertools module contains numerous additional higher-order functions.

The map() and filter() functions are generators; their results must be consumed. Both of them apply a function to a collection of values. In the case of map(), the results of the function are yielded. In the case of filter(), if the result of the function is true, the original value is yielded.

Here's how we can apply a very simple function—so simple we coded it as a lambda—to a sequence of values: ...

Get Python Essentials 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.