Skip to content

Map, Filter, and Reduce in Python

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

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

What is Functional Programming?

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. mapmap, filterfilter, and reducereduce are the three building blocks.

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

The map() Function

map()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()map() function in Python is as follows:

Syntax
map(function, iterable)
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))
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]
command
C:\Users\Your Name> python map_basic.py
[1, 4, 9, 16]

Most often mapmap is used with a lambdalambda:

map_lambda.py
numbers = [1, 2, 3, 4]
squared = map(lambda n: n * n, numbers)
print(list(squared))
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))
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]
command
C:\Users\Your Name> python map_multi.py
[11, 22, 33]

The filter() Function

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

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

Syntax
filter(function, iterable)
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))
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]
command
C:\Users\Your Name> python filter_basic.py
[2, 4, 6]

The reduce() Function

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

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

Syntax
from functools import reduce
reduce(function, iterable, initializer)
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)
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
command
C:\Users\Your Name> python reduce_sum.py
10

Diagram:

diagram reduce mermaid
How reduce accumulates a list into one value

reducereduce 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)
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
command
C:\Users\Your Name> python reduce_init.py
24

map / filter vs. Comprehensions

A list comprehension can do everything mapmap and filterfilter 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)
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]
command
C:\Users\Your Name> python vs_comprehension.py
[4, 16, 36] [4, 16, 36]

Visualize it

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

Conclusion

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


Try it: Map, Filter, and Reduce Exercises

Exercise 1 – map()

Exercise 2 – filter()

Exercise 3 – reduce()

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did