Give Python a collection of values, and you'll almost always want to do one of three things to it: change every item, pick out some items, or combine everything into one. That's the entire story of map(), filter(), and reduce().
π§ What Is Functional Programming, Really?
Nothing scary β it's just a style where you process data by passing it through functions, rather than manually looping and mutating things.
[1, 2, 3, 4, 5]
β
multiply by 2
β
[2, 4, 6, 8, 10]
Python gives you three built-in tools for exactly this kind of thinking: map(), filter(), and reduce().
πΊοΈ map() β "Do this to EVERY item"
map() transforms every single element the same way.
numbers = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, numbers))
print(result)
# [2, 4, 6, 8, 10]
What's lambda x: x * 2? Just a tiny, unnamed function β shorthand for:
def double(x):
return x * 2
Real example:
names = ["deepika", "rahul", "ananya"]
result = list(map(str.upper, names))
# ['DEEPIKA', 'RAHUL', 'ANANYA']
Interview-ready answer: "
map()applies a function to every element of an iterable and produces the transformed results."
Use it when: you're converting/transforming every item the same way β uppercasing strings, doubling numbers, converting units, extracting a field from each object.
π§ MAP = MODIFY EVERYONE
π filter() β "Keep only the ones I want"
filter() doesn't change anything β it selects items based on a condition.
numbers = [1, 2, 3, 4, 5, 6]
result = list(filter(lambda x: x % 2 == 0, numbers))
print(result)
# [2, 4, 6]
For each number, lambda x: x % 2 == 0 asks "is this even?" β only the ones that return True survive:
1 β False β β
2 β True β β
3 β False β β
4 β True β β
5 β False β β
6 β True β β
Interview-ready answer: "
filter()keeps only the elements for which the condition returnsTrue."
π§ FILTER = CHOOSE
π map() vs filter() β the quick gut-check
map() |
filter() |
|
|---|---|---|
| Job | Transform every item | Select some items |
| Item count | Stays the same | Can shrink |
| Mental model | "Do something to everyone" | "Keep only those who qualify" |
β reduce() β "Combine everything into ONE result"
The trickiest of the three β because instead of many outputs, you get one.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, numbers)
print(result)
# 15
It works step by step:
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
Why two parameters (x, y) instead of one? Because reduce() is always combining two things at a time: the accumulated result so far (x) and the next item (y).
Note: unlike map() and filter(), reduce() isn't built-in by default β you import it from functools.
π§ REDUCE = COMBINE
π Real-World Example: Running a Pizza Shop
prices = [100, 200, 300, 400]
map() β bump every price by βΉ50:
new_prices = list(map(lambda x: x + 50, prices))
# [150, 250, 350, 450]
filter() β show only pizzas above βΉ200:
expensive = list(filter(lambda x: x > 200, prices))
# [300, 400]
reduce() β get the total bill:
from functools import reduce
total = reduce(lambda x, y: x + y, prices)
# 1000
π The Cheat Sheet
| Function | Meaning | Question it answers | Result |
|---|---|---|---|
map() |
Transform | "What should I do to every item?" | Transformed values |
filter() |
Select | "Which items should I keep?" | Selected values |
reduce() |
Combine | "How can I combine everything?" | Usually one value |
MAP β TRANSFORM
FILTER β SELECT
REDUCE β COMBINE
β οΈ A Small Gotcha
map() and filter() don't hand you a plain list directly β they return a lazy map/filter object. That's why you'll almost always wrap them in list(...):
result = list(map(lambda x: x * 2, numbers))
result = list(filter(lambda x: x % 2 == 0, numbers))
π€ "Why not just use a for loop?"
You absolutely can:
result = []
for x in numbers:
result.append(x * 2)
This does the exact same thing as list(map(lambda x: x * 2, numbers)). map()/filter() can express intent more concisely for simple transformations β but they're not automatically "better." Python developers often reach for a plain loop when the logic gets more complex or less readable as a one-liner.
β¨ The More "Pythonic" Alternative: List Comprehensions
For simple cases, most Python code actually favors list comprehensions over map()/filter():
# instead of:
result = list(map(lambda x: x * 2, numbers))
# you'll often see:
result = [x * 2 for x in numbers]
# instead of:
result = list(filter(lambda x: x % 2 == 0, numbers))
# you'll often see:
result = [x for x in numbers if x % 2 == 0]
Still β understanding map(), filter(), and reduce() matters, because they're the foundation of functional-programming thinking in Python (and show up constantly in interviews and other languages too).
π― One Sentence to Remember
map() changes every item, filter() picks the items that qualify, and reduce() collapses everything down into a single result.
Top comments (0)