Skip to content

Map, Filter, & Reduce

Python is a multi-paradigm language. While we often think in “steps” (Imperative) or “objects” (OOP), the Functional paradigm focuses on the transformation of data through pure functions.

The trio of map, filter, and reduce are the cornerstones of this style. They allow you to process entire collections without writing a single for or while loop in your own code.


map(function, iterable) applies a function to every item in a collection.

map_usage.py
prices = [10.99, 5.50, 20.00]
# Add a 10% tax to every price
taxed_prices = map(lambda p: p * 1.1, prices)
# map returns a 'map object' (an iterator), not a list!
print(taxed_prices) # <map object at 0x...>
print(list(taxed_prices)) # [12.089, 6.05, 22.0]

map is lazy. It doesn’t actually do any math until you iterate over the result. This means you can “map” a function across a billion items without using any extra memory until the moment you need the results.


filter(function, iterable) keeps only the items where the function returns a Truthy value.

filter_usage.py
logs = ["INFO: Start", "ERROR: Disk Full", "INFO: Processed"]
# Keep only the error messages
errors = filter(lambda s: s.startswith("ERROR"), logs)
print(list(errors)) # ["ERROR: Disk Full"]

reduce(function, iterable) reduces a sequence to a single value. It applies the function cumulatively: f(f(f(a, b), c), d).

Note: In Python 3, this was moved to the functools module because Guido van Rossum preferred explicit loops or sum() for most use cases.

reduce_usage.py
from functools import reduce
nums = [1, 2, 3, 4]
# Calculate factorial: 1 * 2 * 3 * 4
product = reduce(lambda x, y: x * y, nums)
print(product) # 24

In modern Python, you have a choice.

Functional StyleList Comprehension
map(f, data)[f(x) for x in data]
filter(f, data)[x for x in data if f(x)]

When to use map/filter?

  1. When you already have a pre-defined function (no need for lambda).
  2. When you want to maintain Lazy Evaluation (returning an iterator rather than a full list).

When to use Comprehensions?

  1. Most other cases. They are generally considered more readable and “Pythonic.”

FunctionInputOutputPurpose
mapFunction + IterableIteratorTransform every item.
filterBoolean Function + IterableIteratorRemove unwanted items.
reduce2-Arg Function + IterableSingle ValueCombine all items into one.