DEV Community

Deepika Pusala
Deepika Pusala

Posted on

Week 04 - Task 2.1: Functional Programming in Python: map(), filter(), reduce() πŸ—ΊοΈπŸ”Žβž•

 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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

What's lambda x: x * 2? Just a tiny, unnamed function β€” shorthand for:

def double(x):
    return x * 2
Enter fullscreen mode Exit fullscreen mode

Real example:

names = ["deepika", "rahul", "ananya"]
result = list(map(str.upper, names))
# ['DEEPIKA', 'RAHUL', 'ANANYA']
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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  β†’ βœ…
Enter fullscreen mode Exit fullscreen mode

Interview-ready answer: "filter() keeps only the elements for which the condition returns True."

🧠 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
Enter fullscreen mode Exit fullscreen mode

It works step by step:

1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

map() β€” bump every price by β‚Ή50:

new_prices = list(map(lambda x: x + 50, prices))
# [150, 250, 350, 450]
Enter fullscreen mode Exit fullscreen mode

filter() β€” show only pizzas above β‚Ή200:

expensive = list(filter(lambda x: x > 200, prices))
# [300, 400]
Enter fullscreen mode Exit fullscreen mode

reduce() β€” get the total bill:

from functools import reduce
total = reduce(lambda x, y: x + y, prices)
# 1000
Enter fullscreen mode Exit fullscreen mode

πŸ† 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
Enter fullscreen mode Exit fullscreen mode

⚠️ 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))
Enter fullscreen mode Exit fullscreen mode

πŸ€” "Why not just use a for loop?"

You absolutely can:

result = []
for x in numbers:
    result.append(x * 2)
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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)