Mastering Functional Programming in Python with map, filter, and reduce Functions

Introduction

Python provides three built-in functions for functional programming: map, filter, and reduce. These functions are useful for applying a function to every element in a sequence, filtering elements that meet a certain condition, and combining a sequence of values into a single value, respectively.

Map

The map function applies a given function to each element of an iterable (such as a list, tuple, or string), and returns a new iterable with the results. Here’s the syntax of the map function:

map(function, iterable)

The function argument is a function that takes one or more arguments, and the iterable argument is an iterable object that contains the elements to be transformed.

For example, suppose we have a list of integers, and we want to square each element of the list:

numbers = [1, 2, 3, 4, 5]
squares = map(lambda x: x**2, numbers)
print(list(squares))

This will output:

[1, 4, 9, 16, 25]

In this example, we use a lambda function to define the function to be applied to each element of the list.

Filter

The filter function applies a given function to each element of an iterable, and returns a new iterable containing only the elements for which the function returns True. Here’s the syntax of the filter function:

filter(function, iterable)

The function argument is a function that takes one argument, and returns a boolean value indicating whether the argument meets the filter criteria. The iterable argument is an iterable object that contains the elements to be filtered.

For example, suppose we have a list of integers, and we want to filter out the even numbers:

numbers = [1, 2, 3, 4, 5]
odds = filter(lambda x: x % 2 == 1, numbers)
print(list(odds))

This will output:

[1, 3, 5]

In this example, we use a lambda function to define the function that filters out the even numbers.

Reduce

The reduce function applies a given function to the elements of an iterable in a cumulative way, and returns a single value as the result. Here’s the syntax of the reduce function:

reduce(function, iterable, initial)

The function argument is a function that takes two arguments, and returns a single value as the result of applying the function to the two arguments. The iterable argument is an iterable object that contains the elements to be reduced. The initial argument is an optional value that is used as the first argument to the function. If initial is not specified, the first two elements of the iterable are used as the first two arguments to the function.

For example, suppose we have a list of integers, and we want to compute their sum using the reduce function:

from functools import reduce

numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(total)

This will output:

15

In this example, we use a lambda function to define the function that adds two numbers together. We also import the reduce function from the functools module, which is required to use the reduce function in Python 3.x.

Recap

mapApplies a given function to each element of an iterable and returns a new iterable with the results.
filterFilters out the elements of an
iterable that do not meet a
certain condition and returns a
new iterable with the remaining
elements.
reduceApplies a given function to the elements of an iterable in a cumulative way and returns a single value as the result.

Hello, I’m Anuj. I make and teach software.

My website is free of advertisements, affiliate links, tracking or analytics, sponsored posts, and paywalls.
Follow me on LinkedIn, X (twitter) to get timely updates when I post new articles.
My students are the reason this website exists. ❤️

Feedback Display