Tutorials Python Python map, filter & reduce
Ch 47 / 58
Python Intermediate 12 min read Chapter 47 of 58

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.

What you'll learn in this chapter
map() — transform every element of an iterable
filter() — keep elements that pass a predicate
reduce() — accumulate all elements to one value
lambda functions with map/filter/reduce
When to use list comprehensions instead
Chaining and functional pipelines
Advertisement

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, filter, reduce — what each operation does
map(fn, iterable) Transform — apply fn to every element
Input: [1, 2, 3, 4] → apply fn(x) to each → Output: same number of elements, all transformed.
[1,2,3,4] → map(x²) → [1,4,9,16] ← same length, values changed
filter(predicate, iterable) Select — keep elements where predicate is True
Input: [1, 2, 3, 4, 5] → keep where fn(x) is True → Output: fewer (or same) elements.
[1,2,3,4,5] → filter(even) → [2,4] ← shorter, same values
reduce(fn, iterable) Accumulate — fold all elements into one value
Input: [1, 2, 3, 4] → apply fn(acc, x) repeatedly → Output: single accumulated value.
[1,2,3,4] → reduce(+) → 10 ← single value

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.

Python — map_examples.py
# ── 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.

Python — filter_examples.py
# ── 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]
Advertisement
K2infocom Python Masterclass & Backend Development
Master Python, Django & Backend APIs with AI
Go from absolute beginner to building production-ready REST APIs, web applications, and ML pipelines. Real projects, live mentoring in Vadodara, and placement support included.
2,400+
Students Enrolled
4.9 ⭐
Google Rating
6 Months
Full Program
100%
Placement Support
Python 3 Django & DRF FastAPI PostgreSQL Machine Learning Docker & AWS Real Projects AI Tools

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.

reduce([1,2,3,4], +) — step-by-step accumulation
fn(1, 2) = 3 ← step 1: first two elements
▼
accumulator = 3
▼
fn(3, 3) = 6 ← step 2: acc + next element
▼
accumulator = 6
▼
fn(6, 4) = 10 ← step 3: acc + next element
▼
Final result: 10 ✅
Python — reduce_examples.py
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.

Python — lambda_functions.py
# ── 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
Python — map_vs_comprehension.py
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.

Python — functional_pipeline.py
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.

▶
Online Python 3 Compiler
Write, run and debug Python code in your browser. No installation required. Test every map, filter and reduce example instantly.
Open Compiler
Replit
Full IDE online
Google Colab
Free GPU notebooks
JupyterLite
Notebooks in browser
Programiz
Beginner-friendly
Chapter Summary
  • map(fn, iterable) applies fn to every element, returning a lazy map object. Accepts multiple iterables for parallel transformation. Wrap with list() for a list.
  • filter(fn, iterable) keeps only elements where fn(x) returns True. filter(None, ...) removes all falsy values. Returns a lazy filter object.
  • reduce(fn, iterable, initial) from functools applies a 2-argument function cumulatively, producing a single accumulated value. Always provide an initializer for safety with empty iterables.
  • lambda args: expression creates 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. Use map()/filter() when passing as arguments, combining with multiple iterables, or building lazy pipelines.
  • Chain map() and filter() together for data pipelines — no intermediate lists are built.
  • The operator module provides named versions of operators: operator.add, operator.mul — cleaner than lambda a, b: a + b.
Free Resource
Download our Python map, filter & reduce Cheat Sheet
map(), filter(), reduce(), lambda, vs comprehensions, chaining & functional pipelines — one printable page.
Get Sheet
Advertisement
Quick Check — Test Your Understanding
Question & Answer
✓ Answer

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
✓ Answer

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, [], {}, ().

✓ Answer

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!
✓ Answer

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.
✓ Answer

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
MCQ 1 of 3
What does list(map(lambda x: x*2, [1, 2, 3])) return?
MCQ 2 of 3
Which module does reduce() live in Python 3?
MCQ 3 of 3
What does list(filter(None, [0, "", 1, None, "hi", False])) return?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel