Understanding Higher-Order Functions With Simple Examples in Python

Master the map, filter, and reduce functions

Man jumping on the street
Photo by Ady April on Pexels.

Higher-order functions are functions that take a function as a parameter and/or return a function as an output.

A few useful higher-order functions are map(), filter(), and reduce(). map() and filter() are built-in functions, whereas reduce() is contained in functools() module.

Let’s learn about map(), filter(), and reduce() in this article.


map() is used to apply a function to each item in the iterable at the same time.
map() will return a map object that is an iterator:


Calculating the square of all numbers in the iterable:


  • square=map(lambda x:x*2, num): We can pass the lambda function or user_defined function. The lambda function’s syntax is lambda parameters : expression.
  • map() returns a map object that is an iterator. We can convert the iterator (map object) to an iterable like list using a list constructor (print(list(square))).

map vs. for loop

Solving the same problem (square of numbers) using a for loop:

for i in num:
print (square)
  • We have to iterate through the iterable (list) using a for loop and append the result in a new list.
  • Sequential traversal: We have to iterate through an iterable (length n) n times.

map vs. list comprehension

Solving the same problem (square of numbers) using list comprehension:

result=[i**2 for i in num]
print (result)
#Output:[1, 4, 9, 16, 25]

List comprehension will return a list and not an iterator, whereas map() will return a map object that is an iterator.

We know that iterators can create on-demand sequences requiring little memory vs. lists that require more memory to store data. If you are working with large data, list comprehensions are not useful. map() can be used to return a map object that is an iterator and computes the values as necessary, not needing to materialize all the values at once.

map vs. generator expression

A generator expression is used when we work with a large amount of data when compared to list comprehension. A generator expression returns an iterator.

The performance improvement from the use of generators is the result of the lazy (on-demand) generation of values, which translates to lower memory usage.

Calculating the square of all numbers in the iterable:


When should we prefer map() over a generator expression?

Both map() and generator expressions only return an iterator.

  • map syntax: map(function,iterable)
  • generator expression syntax: (expression for item in iterable if conditional)

When the logic for list comprehension/a generator expression is too complex to make them understandable, it’s better to use map().

In the map() function, we keep the function separate, which makes the design very clear.

Another difference between map() and generator expressions is in the map() function. We can reuse a lambda or function definition.

map() vs. starmap()

Applying a function to two iterables using map():


According to the Python docs, starmap() is “used instead of map() when argument parameters are already grouped in tuples from a single iterable (the data has been ‘pre-zipped’).”

Diagram showing difference between map() and starmap()


map vs. starmap(). Photo by the author.



filter() is used to filter the elements from the iterable that matches a certain condition:


Filtering the even numbers in the iterable:


filter() also returns a filter object that is an iterator. It doesn’t immediately go over all the elements but will return the next value when we ask for it
( using next()):


If we want all results at once, we can convert it to a list using the list() constructor.

filter() vs. filterfalse()

“Make an iterator that filters elements from iterable returning only those for which the predicate is False.” — Python docs


Filtering the elements that don’t start with “r”:



The functools module provides the following function:


Applying the function of two arguments cumulatively to the items of the iterable, from left to right, so as to reduce the iterable to a single value:

functools.reduce(function, iterable)

Python provides some built-in reducing functions:

  • max() returns the largest number in the iterable.
  • min() returns the smallest number in the iterable.
  • sum() returns the sum of all numbers in an iterable.

Finding the product of all elements on the list:

from functools import reduce
num2=reduce(lambda x,y:x*y,num1)
print (num2)#Output:120

reduce() vs. accumulate()

“Make an iterator that returns accumulated sums, or accumulated results of other binary functions (specified via the optional func argument).” — Python docs


Finding the product of the numbers in an iterable and finding the running accumulated value:

Comparison between reduce() and accumulate()


reduce() vs accumulate()


Key Takeaways

  • map(): Apply function to each element in the iterable (map(function,iterable)).
  • filter(): Filter the elements in the iterable that match certain conditions
  • reduce(): It takes successive elements in the iterable and combines them in a certain way (reduce(function,iterable)).

Watch this space for more articles on Python and DataScience. If you like to read more of my tutorials, follow me on Medium, LinkedIn, Twitter.


Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount


Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s