Lesson 07
Python Special Functions — filter(), map(), and reduce() with Examples
Master Python filter(), map(), and reduce(): extract matching values, transform every item, and combine a list into one result with lambda.
What you will learn
filter()— keep matching valuesmap()— transform every itemreduce()(fromfunctools) — combine into one value- Why
filter()andmap()return lazy iterators (uselist()) - The real-world filter → map → reduce pipeline
The big picture
Master Python's three functional programming tools: filter (extract), map (transform), and reduce (combine into one value).
This continues from Lambda Functions.
Imagine a list of employee salaries. Each special function does a different job.
| Function | Think of it as… |
|---|---|
filter() |
Keep what matches |
map() |
Change every item |
reduce() |
Combine everything |
Overview comparison
| Function | Module | Purpose | Output |
|---|---|---|---|
filter() |
Built-in | Extract matching elements | Iterator |
map() |
Built-in | Transform each element | Iterator |
reduce() |
functools |
Combine into one value | Single value |
filter()andmap()return lazy iterators.reduce()returns one final result. Convert iterators withlist()when you need to print them.
1. filter() — extract elements
filter() keeps only the elements where a condition is True.
Think of a security guard checking IDs.
Syntax:
filter(function, iterable)The function must return True or False.
Positive numbers
nums = [10, -30, 40, -67, 23]
positives = list(filter(lambda x: x > 0, nums))
print(positives)Output:
[10, 40, 23]Even and odd
sample = [1, 2, 3, 4, 5, 6, 7, 8]
evens = list(filter(lambda x: x % 2 == 0, sample))
odds = list(filter(lambda x: x % 2 != 0, sample))
print(evens)
print(odds)| Condition | Output |
|---|---|
| Even | [2, 4, 6, 8] |
| Odd | [1, 3, 5, 7] |
Student pass filter
students = [
("Alice", 82),
("Bob", 48),
("Carol", 91),
]
passed = list(filter(lambda s: s[1] >= 50, students))
print(passed)Only Alice and Carol remain.
2. map() — transform every element
map() changes every item into something new. It never drops elements.
Syntax:
map(function, iterable)Salary hike
salaries = [30000, 45000, 60000]
hiked = list(map(lambda s: s * 1.5, salaries))
print(hiked)Output:
[45000.0, 67500.0, 90000.0]Every salary is transformed.
Square every number
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, nums))
print(squares)Output: [1, 4, 9, 16]
Temperature conversion
celsius = [0, 20, 37, 100]
fahrenheit = list(map(lambda c: (c * 9 / 5) + 32, celsius))
print(fahrenheit)| Celsius | Fahrenheit |
|---|---|
| 0 | 32 |
| 20 | 68 |
| 37 | 98.6 |
| 100 | 212 |
3. reduce() — one value
Unlike map() and filter(), reduce() returns one result. It lives in functools.
import functoolsSum
total = functools.reduce(lambda a, b: a + b, [10, 20, 30, 40, 50])
print(total)Output: 150
Maximum
maximum = functools.reduce(
lambda a, b: a if a > b else b,
[10, 5, 30, 40],
)
print(maximum)Each step compares two numbers.
Output: 40
Product
product = functools.reduce(lambda a, b: a * b, [1, 2, 3, 4, 5])
print(product)1 × 2 × 3 × 4 × 5 = 120 — the same pattern as factorial.
The functional pipeline
This is the most important real-world pattern. Imagine company payroll.
- Filter — keep salaries above ₹25,000
- Map — give everyone a 20% hike
- Reduce — calculate total payroll
import functools
raw = [15000, 45000, 30000, 80000, 25000, 90000]
above = list(filter(lambda s: s > 25000, raw))
after = list(map(lambda s: s * 1.20, above))
payroll = functools.reduce(lambda a, b: a + b, after)
print(above)
print(after)
print(payroll)Output:
[45000, 30000, 80000, 90000]
[54000.0, 36000.0, 96000.0, 108000.0]
294000.0This pipeline appears in data science, ETL, analytics, and backend processing.
When to use which
| Goal | Use |
|---|---|
| Keep matching values | filter() |
| Modify every value | map() |
| Find total, max, min | reduce() |
| Remove failed students | filter() |
| Salary increment | map() |
| Payroll total | reduce() |
Common mistakes
Forgetting list() — filter(...) and map(...) return iterator objects, not lists.
Using reduce without importing — from functools import reduce or import functools.
Wrong lambda for filter — filter should return True or False, not transformed values.
Using map for filtering — map() changes values; it never removes elements.
Interview cheat sheet
| Question | Answer |
|---|---|
| Which keeps elements? | filter() |
| Which transforms elements? | map() |
| Which returns one value? | reduce() |
reduce() module |
functools |
filter() return type |
Filter iterator |
map() return type |
Map iterator |
Need list()? |
Yes, usually |
Final memory diagram
Filter → Map → Reduce is one of the most common functional pipelines in Python, Pandas, APIs, and data processing.
Practice in the playground
The playground at the bottom already runs filter, map, reduce, and the payroll pipeline. Try:
- Change the pass mark from
50to80— only Carol should pass. - Change the hike from
1.5to2— 30000 becomes60000.0. - Comment out
list(...)aroundfilterand print it — you should see afilterobject.
This page is the Functions intro. For the SPECIAL FUNCTIONS lab — palindrome filter(), zip tables, two-list map(), functools.reduce internals, and 10%/20% salary bands — continue in Lambda and Special Functions.
Back to the Functions overview.
Loading playground…
Check your understanding
Special functions quiz
What is the purpose of filter() in Python?
1 / 10