Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate50 min

What you will learn

  • filter() — keep matching values
  • map() — transform every item
  • reduce() (from functools) — combine into one value
  • Why filter() and map() return lazy iterators (use list())
  • 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.

Salaries list then filter then map then reduce
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() and map() return lazy iterators. reduce() returns one final result. Convert iterators with list() 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.

Positive filter keeps 10, 40, 23 and drops negatives

Syntax:

python
filter(function, iterable)

The function must return True or False.

Positive numbers

python
nums = [10, -30, 40, -67, 23]

positives = list(filter(lambda x: x > 0, nums))
print(positives)

Output:

code
[10, 40, 23]
Each value becomes True keep or False drop

Even and odd

python
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

python
students = [
    ("Alice", 82),
    ("Bob", 48),
    ("Carol", 91),
]

passed = list(filter(lambda s: s[1] >= 50, students))
print(passed)
Alice 82 pass, Bob 48 fail, Carol 91 pass

Only Alice and Carol remain.


2. map() — transform every element

map() changes every item into something new. It never drops elements.

Original 100 200 300 400 mapped plus 50 percent to 150 300 450 600

Syntax:

python
map(function, iterable)

Salary hike

python
salaries = [30000, 45000, 60000]
hiked = list(map(lambda s: s * 1.5, salaries))
print(hiked)

Output:

code
[45000.0, 67500.0, 90000.0]

Every salary is transformed.

Square every number

python
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, nums))
print(squares)
1 becomes 1, 2 becomes 4, 3 becomes 9, 4 becomes 16

Output: [1, 4, 9, 16]

Temperature conversion

python
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.

python
import functools

Sum

python
total = functools.reduce(lambda a, b: a + b, [10, 20, 30, 40, 50])
print(total)
Accumulator 10 plus 20 is 30, then 60, 100, 150

Output: 150

Maximum

python
maximum = functools.reduce(
    lambda a, b: a if a > b else b,
    [10, 5, 30, 40],
)
print(maximum)

Each step compares two numbers.

Tournament: 10 vs 5 is 10, 30 vs 40 is 40, winner 40

Output: 40

Product

python
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.

  1. Filter — keep salaries above ₹25,000
  2. Map — give everyone a 20% hike
  3. Reduce — calculate total payroll
python
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)
Pipeline from raw salaries through filter map reduce to 294000 rupees

Output:

code
[45000, 30000, 80000, 90000]
[54000.0, 36000.0, 96000.0, 108000.0]
294000.0

This 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

Raw data to Filter to Map to Reduce one value

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:

  1. Change the pass mark from 50 to 80 — only Carol should pass.
  2. Change the hike from 1.5 to 2 — 30000 becomes 60000.0.
  3. Comment out list(...) around filter and print it — you should see a filter object.

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