Skip to content

Map, Filter, and Reduce in Python

Mastering Map, Filter, and Reduce in Python: A Comprehensive Guide

Section titled “Mastering Map, Filter, and Reduce in Python: A Comprehensive Guide”

map(), filter(), and reduce() are the classic tools of functional programming. Each takes a function and applies it across an iterable: map transforms every item, filter selects items that pass a test, and reduce combines all items into a single value. They pair naturally with lambda functions. In this guide, we’ll explore each one with examples and compare them to comprehensions.

Functional programming treats functions as values you can pass around. Instead of writing explicit loops, you describe what transformation to apply and let the tool handle the iteration. map, filter, and reduce are the three building blocks.

ToolPurposeResult
map(func, iterable)Apply func to every itemAn iterator of transformed items
filter(func, iterable)Keep items where func returns TrueAn iterator of kept items
reduce(func, iterable)Combine items pairwise into one valueA single value

map() applies a function to every element of an iterable and returns a map object (an iterator). Convert it to a list to see the results.

The syntax of the map() function in Python is as follows:

Syntax
map(function, iterable)
map_basic.py
numbers = [1, 2, 3, 4]
 
def square(n):
    return n * n
 
squared = map(square, numbers)
print(list(squared))

Output:

command
C:\Users\Your Name> python map_basic.py
[1, 4, 9, 16]

Most often map is used with a lambda:

map_lambda.py
numbers = [1, 2, 3, 4]
squared = map(lambda n: n * n, numbers)
print(list(squared))

You can also pass multiple iterables — the function then receives one item from each:

map_multi.py
a = [1, 2, 3]
b = [10, 20, 30]
sums = map(lambda x, y: x + y, a, b)
print(list(sums))

Output:

command
C:\Users\Your Name> python map_multi.py
[11, 22, 33]

filter() keeps only the elements for which the function returns True. It also returns an iterator.

The syntax of the filter() function in Python is as follows:

Syntax
filter(function, iterable)
filter_basic.py
numbers = [1, 2, 3, 4, 5, 6]
 
evens = filter(lambda n: n % 2 == 0, numbers)
print(list(evens))

Output:

command
C:\Users\Your Name> python filter_basic.py
[2, 4, 6]

reduce() repeatedly applies a function of two arguments to the items of an iterable, accumulating them into a single value. Unlike map and filter, it lives in the functools module and must be imported.

The syntax of the reduce() function in Python is as follows:

Syntax
from functools import reduce
reduce(function, iterable, initializer)
reduce_sum.py
from functools import reduce
 
numbers = [1, 2, 3, 4]
total = reduce(lambda acc, n: acc + n, numbers)
print(total)

Output:

command
C:\Users\Your Name> python reduce_sum.py
10

Diagram:

diagram reduce mermaid
How reduce accumulates a list into one value

reduce accepts an optional initializer — the starting value for the accumulator. It is safer because it defines the result for an empty iterable.

reduce_init.py
from functools import reduce
 
numbers = [1, 2, 3, 4]
product = reduce(lambda acc, n: acc * n, numbers, 1)   # start at 1
print(product)

Output:

command
C:\Users\Your Name> python reduce_init.py
24

A list comprehension can do everything map and filter do, and is often considered more Pythonic and readable.

vs_comprehension.py
numbers = [1, 2, 3, 4, 5, 6]
 
# map + filter
result_fp = list(map(lambda n: n * n, filter(lambda n: n % 2 == 0, numbers)))
 
# equivalent comprehension
result_comp = [n * n for n in numbers if n % 2 == 0]
 
print(result_fp, result_comp)

Output:

command
C:\Users\Your Name> python vs_comprehension.py
[4, 16, 36] [4, 16, 36]

reduce is the trickiest of the three: it carries an accumulator across the list, combining it with one item at a time to collapse everything into a single value. Here it folds a list into its sum — watch the accumulator grow as each item is consumed:

sketch reduce folds a list into one value p5.js
An accumulator is combined with each item in turn, collapsing the whole list to one result.

map, filter, and reduce apply a function across an iterable in three different ways: map transforms every item, filter selects the items that pass a test, and reduce collapses the whole iterable into one value. map and filter are built in and return lazy iterators; reduce lives in functools. While comprehensions often replace map and filter more readably, reduce remains the go-to for accumulation. For more hands-on examples and in-depth tutorials, explore our resources on Python Central Hub!


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading