O'Reilly logo

Python Essentials by Steven F. Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required