Python map(), filter() & reduce()
map(), filter(), and reduce() are Python's core
functional programming tools for processing sequences. map() transforms every element, filter()
keeps elements that pass a test, and reduce() accumulates all elements
into a single value. While list comprehensions are often more Pythonic, these functions become essential in
functional pipelines, when working with itertools, passing transformations
as arguments, or processing very large datasets lazily.
Overview — The Three Core Functional Tools
All three functions are lazy — they return iterators, not lists. Wrap
them in list() to get all results, or iterate over them directly for
memory efficiency.
map() — Transform Every Element
map(function, iterable) applies a function to each
element and returns a map object (lazy iterator). It can take multiple iterables and applies
the function in parallel across them.
# ── Basic map() ───────────────────────────────────────
nums = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, nums)
print(type(squared)) # <class 'map'> — lazy iterator
print(list(squared)) # [1, 4, 9, 16, 25]
# ── With a named function ─────────────────────────────
def celsius_to_fahrenheit(c):
return c * 9/5 + 32
temps_c = [0, 20, 37, 100]
temps_f = list(map(celsius_to_fahrenheit, temps_c))
print(temps_f) # [32.0, 68.0, 98.6, 212.0]
# ── map() with built-in functions ─────────────────────
strings = [" hello ", " world ", " python "]
cleaned = list(map(str.strip, strings))
print(cleaned) # ['hello', 'world', 'python']
print(list(map(str.upper, ["a", "b", "c"]))) # ['A', 'B', 'C']
print(list(map(int, ["1", "2", "3"]))) # [1, 2, 3]
# ── map() with multiple iterables ────────────────────
a = [1, 2, 3]
b = [10, 20, 30]
print(list(map(lambda x, y: x + y, a, b))) # [11, 22, 33]
print(list(map(pow, [2,3,4], [1,2,3]))) # [2, 9, 64]
# ── Lazy evaluation — process large data efficiently ──
big_data = range(10_000_000) # no memory used yet
processed = map(lambda x: x * 2, big_data) # still no memory!
first_five = [next(iter(processed)) for _ in range(5)]
print(first_five) # [0, 2, 4, 6, 8]
filter() — Keep Elements That Pass a Test
filter(function, iterable) returns only the elements
for which the function returns True. If function is None, it removes falsy values.
Returns a lazy filter object.
# ── Basic filter() ────────────────────────────────────
nums = [1, 2, 3, 4, 5, 6, 7, 8]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # [2, 4, 6, 8]
# ── With a named predicate ────────────────────────────
def is_positive(n): return n > 0
data = [-3, -1, 0, 2, 5, -2, 7]
positive = list(filter(is_positive, data))
print(positive) # [2, 5, 7]
# ── filter(None, ...) — remove falsy values ───────────
mixed = [0, "", None, 1, "hello", False, 42, [], {"a":1}]
truthy = list(filter(None, mixed))
print(truthy) # [1, 'hello', 42, {'a': 1}]
# ── Filter strings, dicts ─────────────────────────────
words = ["python", "java", "rust", "go", "typescript"]
long = list(filter(lambda w: len(w) > 4, words))
print(long) # ['python', 'typescript']
users = [
{"name": "Alice", "active": True},
{"name": "Bob", "active": False},
{"name": "Carol", "active": True},
]
active = list(filter(lambda u: u["active"], users))
print([u["name"] for u in active]) # ['Alice', 'Carol']
# ── Filter with multiple conditions ───────────────────
scores = [45, 78, 92, 33, 67, 88, 55]
passing = list(filter(lambda s: 60 <= s <= 100, scores))
print(passing) # [78, 92, 67, 88]
reduce() — Accumulate to a Single Value
functools.reduce(function, iterable, initializer)
applies a two-argument function cumulatively — the first call uses elements 1 and 2, the result is then used
with element 3, and so on until the sequence is exhausted.
from functools import reduce
# ── Basic reduce() ────────────────────────────────────
nums = [1, 2, 3, 4, 5]
# Sum
total = reduce(lambda acc, x: acc + x, nums)
print(total) # 15
# Product
prod = reduce(lambda acc, x: acc * x, nums)
print(prod) # 120
# Maximum (manually)
max_val = reduce(lambda a, b: a if a > b else b, nums)
print(max_val) # 5
# ── With initializer ──────────────────────────────────
empty = []
total = reduce(lambda acc, x: acc + x, empty, 0)
print(total) # 0 ← initializer prevents error on empty
total = reduce(lambda acc, x: acc + x, [10, 20], 100)
print(total) # 130 (100 + 10 + 20)
# ── Use named functions ───────────────────────────────
import operator
print(reduce(operator.add, nums)) # 15 — cleaner than lambda
print(reduce(operator.mul, nums)) # 120
# ── Flatten a list of lists ───────────────────────────
nested = [[1,2], [3,4], [5,6]]
flattened = reduce(lambda acc, x: acc + x, nested)
print(flattened) # [1, 2, 3, 4, 5, 6]
# ── Build a dict with reduce ──────────────────────────
pairs = [("a", 1), ("b", 2), ("c", 3)]
d = reduce(lambda acc, kv: {**acc, kv[0]: kv[1]}, pairs, {})
print(d) # {'a': 1, 'b': 2, 'c': 3}
# Note: dict() or dict comprehension is cleaner for this use case
Lambda Functions — Anonymous Function Expressions
lambda arguments: expression creates a small
anonymous function inline. Lambdas are perfect for short transformations passed to map(), filter(), reduce(), and sorted(). For anything
complex, use a named function.
# ── Lambda syntax ─────────────────────────────────────
double = lambda x: x * 2 # single argument
add = lambda x, y: x + y # multiple arguments
greet = lambda name="World": f"Hi {name}" # default value
negate = lambda x: -x # unary
is_even = lambda x: x % 2 == 0 # returns bool
print(double(5)) # 10
print(add(3, 4)) # 7
print(greet("Alice")) # Hi Alice
# ── Lambda in sorted() key ────────────────────────────
people = [
{"name": "Charlie", "age": 30},
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 28},
]
by_age = sorted(people, key=lambda p: p["age"])
by_name = sorted(people, key=lambda p: p["name"])
# ── Lambda with map and filter ────────────────────────
nums = [1, 2, 3, 4, 5, 6]
print(list(map(lambda x: x**2, nums))) # [1,4,9,16,25,36]
print(list(filter(lambda x: x > 3, nums))) # [4,5,6]
# ── Conditional expression in lambda ─────────────────
abs_val = lambda x: x if x >= 0 else -x
print(list(map(abs_val, [-3, -1, 0, 2]))) # [3, 1, 0, 2]
# ── When NOT to use lambda ────────────────────────────
# ❌ Complex logic:
# fn = lambda x: (very complex expression) — use def instead
# ❌ Assignments in PEP8:
# f = lambda x: x*2 — name it with def when storing in variable
map/filter vs List Comprehensions
Python's list comprehensions are generally preferred over map()/filter() for readability. But each
has its place:
| Use Case | Preferred | Why |
|---|---|---|
| Simple transform | [x**2 for x in nums] |
More readable, Pythonic |
| Simple filter | [x for x in nums if x>3] |
Single expression |
| Pass as argument | map(fn, data) |
Can't pass a comprehension |
| Lazy/large data | map(fn, data) |
No list built in memory |
| Multiple iterables | map(fn, a, b) |
Cleaner than zip comprehension |
| Accumulate to one | reduce(fn, data) |
No list comprehension alternative |
nums = [1, 2, 3, 4, 5, 6]
# ── Equivalent transforms — prefer comprehension ─────
with_map = list(map(lambda x: x**2, nums))
with_comp = [x**2 for x in nums] # ← more readable
# ── Equivalent filters — prefer comprehension ────────
with_filter = list(filter(lambda x: x > 3, nums))
with_comp = [x for x in nums if x > 3] # ← more readable
# ── Map wins: passing as argument ────────────────────
def process_batch(transform, data):
return list(map(transform, data)) # transform is a function arg
print(process_batch(str.upper, ["a", "b"])) # ['A', 'B']
print(process_batch(lambda x: x*2, [1,2,3])) # [2, 4, 6]
# ── Map wins: multiple iterables ────────────────────
prices = [100, 200, 300]
discounts = [0.1, 0.2, 0.15]
final = list(map(lambda p, d: p * (1 - d), prices, discounts))
print(final) # [90.0, 160.0, 255.0]
# ── Generator expression — lazy like map ──────────────
gen = (x**2 for x in range(1000000)) # O(1) memory — just like map()
Chaining & Functional Pipelines
The real power of map() and filter() emerges when you chain them together into data
pipelines. Since they return iterators, you can compose them without creating intermediate lists.
from functools import reduce
import operator
# ── Chain map + filter + reduce ───────────────────────
data = [" alice ", "", " BOB ", " ", " carol "]
# Step 1: strip whitespace
# Step 2: filter out empty strings
# Step 3: convert to title case
result = list(
map(str.title,
filter(lambda x: len(x) > 0,
map(str.strip, data)))
)
print(result) # ['Alice', 'Bob', 'Carol']
# ── pipeline() helper function ─────────────────────────
def pipeline(data, *fns):
"""Apply a sequence of transformations to data."""
return reduce(lambda d, fn: fn(d), fns, data)
scores = [85, 42, 91, 30, 78, 66]
top_avg = pipeline(
scores,
lambda d: filter(lambda x: x >= 60, d), # passing only
lambda d: list(d),
lambda d: sum(d) / len(d), # average
)
print(f"Average of passing scores: {top_avg:.1f}") # 80.0
# ── Real-world: process CSV rows ─────────────────────
rows = [
{"name": "Alice", "salary": 50000, "dept": "eng"},
{"name": "Bob", "salary": 45000, "dept": "hr"},
{"name": "Carol", "salary": 60000, "dept": "eng"},
]
# Engineers' total salary
eng_total = reduce(
operator.add,
map(lambda r: r["salary"],
filter(lambda r: r["dept"] == "eng", rows))
)
print(f"Engineering payroll: ₹{eng_total:,}") # ₹1,10,000
Practice Python Online — Free Compilers
Don't want to install anything yet? Practice all map, filter and reduce examples from this chapter directly in your browser with these free Python environments.
map(fn, iterable)appliesfnto every element, returning a lazy map object. Accepts multiple iterables for parallel transformation. Wrap withlist()for a list.filter(fn, iterable)keeps only elements wherefn(x)returnsTrue.filter(None, ...)removes all falsy values. Returns a lazy filter object.reduce(fn, iterable, initial)fromfunctoolsapplies a 2-argument function cumulatively, producing a single accumulated value. Always provide an initializer for safety with empty iterables.lambda args: expressioncreates a small anonymous function. Perfect for inline use with map/filter/reduce/sorted. Use named functions for anything complex.- All three functions are lazy (return iterators, not lists) — memory-efficient for large datasets.
- Prefer list comprehensions
[fn(x) for x in data]for simple transforms and filters — more readable and Pythonic. Usemap()/filter()when passing as arguments, combining with multiple iterables, or building lazy pipelines. - Chain
map()andfilter()together for data pipelines — no intermediate lists are built. - The
operatormodule provides named versions of operators:operator.add,operator.mul— cleaner thanlambda a, b: a + b.
map() returns a map object — a lazy iterator
that produces values on demand. It does NOT compute all values upfront or store them in memory.
You wrap with list() when you need all
values at once as a list — for display, indexing, or repeated iteration. But if you're immediately
iterating (e.g. in a for loop or passing to sum()), you don't need
list():
print(sum(map(lambda x: x**2, [1,2,3]))) # no list() needed
When None is passed as the function, filter() tests each element's truthiness and removes all
falsy values:
mixed = [0, "", None, 1, "hello", False, 42]
print(list(filter(None, mixed)))
# [1, 'hello', 42]
Falsy values in Python: 0, "", None, False, [], {}, ().
Without an initializer, reduce() uses the first two elements of
the iterable. If the iterable is empty, there are no elements to work with — Python
raises a TypeError: reduce() of empty iterable with no initial value.
Providing an initializer gives reduce() a
starting accumulator value, making it safe on empty iterables:
reduce(lambda acc, x: acc + x, [], 0) # 0 — safe!
reduce(lambda acc, x: acc * x, [], 1) # 1 — safe!
Use map() instead of a list comprehension in these cases:
- Passing as argument — you can pass
map(fn, data)to another function, but not a comprehension directly. - Multiple iterables —
map(fn, a, b)applies fn to parallel elements from two iterables — cleaner than[fn(x,y) for x,y in zip(a,b)]. - Already-named functions —
map(str.upper, words)is cleaner than[w.upper() for w in words]. - Lazy evaluation — map() is already lazy; generator expressions do the same but map works in more contexts.
operator.add is equivalent to lambda a, b: a + b. The operator
module provides named versions of all Python operators.
Why use it: it's more readable, slightly faster (it's a C function, not a
Python lambda), and communicates intent clearly. Especially useful with reduce():
import operator
reduce(operator.add, [1,2,3]) # 6 — cleaner than lambda
reduce(operator.mul, [1,2,3,4]) # 24
list(map(lambda x: x*2, [1, 2, 3]))
return?reduce() live in Python 3?list(filter(None, [0, "", 1, None, "hi", False])) return?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.