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.
1. map(): The Transformer
Section titled “1. map(): The Transformer”map(function, iterable) applies a function to every item in a collection.
prices = [10.99, 5.50, 20.00]
# Add a 10% tax to every pricetaxed_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]Under the Hood: Lazy Evaluation
Section titled “Under the Hood: Lazy Evaluation”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.
2. filter(): The Gatekeeper
Section titled “2. filter(): The Gatekeeper”filter(function, iterable) keeps only the items where the function returns a Truthy value.
logs = ["INFO: Start", "ERROR: Disk Full", "INFO: Processed"]
# Keep only the error messageserrors = filter(lambda s: s.startswith("ERROR"), logs)
print(list(errors)) # ["ERROR: Disk Full"]3. reduce(): The Aggregator
Section titled “3. reduce(): The Aggregator”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.
from functools import reduce
nums = [1, 2, 3, 4]# Calculate factorial: 1 * 2 * 3 * 4product = reduce(lambda x, y: x * y, nums)print(product) # 244. Context: Functional vs. Comprehensions
Section titled “4. Context: Functional vs. Comprehensions”In modern Python, you have a choice.
| Functional Style | List 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?
- When you already have a pre-defined function (no need for
lambda). - When you want to maintain Lazy Evaluation (returning an iterator rather than a full list).
When to use Comprehensions?
- Most other cases. They are generally considered more readable and “Pythonic.”
5. Summary Table
Section titled “5. Summary Table”| Function | Input | Output | Purpose |
|---|---|---|---|
map | Function + Iterable | Iterator | Transform every item. |
filter | Boolean Function + Iterable | Iterator | Remove unwanted items. |
reduce | 2-Arg Function + Iterable | Single Value | Combine all items into one. |