Functional Programming with map(), filter(), and reduce() in Python
Turn every number into something else, strip certain items out, boil a group of items into a single summary value. Simple tasks, all of…
Functional Programming with map(), filter(), and reduce() in Python

Blog Thumbnail
Turn every number into something else, strip certain items out, boil a group of items into a single summary value. Simple tasks, all of them. But how you do that — loop vs comprehension, or maybe one of these Python builtins: map(), filter(), reduce() — matters when you’re working at non-trivial scale.
Some use cases?
- map: “take this and make it something else”
- filter: “only keep what matters”
- reduce: “just give me one value back”
You’ve seen these somewhere before. Maybe on someone else’s code review. Maybe briefly in a flattened lambda line you barely noticed.
Check my Youtube Video
[embed]
Here we’re going to focus on each. Unevenly. Honestly. With fresh examples you’d actually write, no filler.
What even is map()
Alright. You got a list. You want to square all values in it.
Straightforward:
nums = [2, 4, 6]
sq = []
for n in nums:
sq.append(n * n)
now same idea using map():
def squared(n):
return n * n
nums = [2, 4, 6]
mapped = map(squared, nums)
But if you print mapped:
print(mapped)
It’s not a list. It’s an iterator.
So:
print(list(mapped)) # [4, 16, 36]
You get your result by consuming it. Once looped over or collected, it’s empty.
Try with a lambda:
nums = [2, 3, 4]
output = map(lambda x: x * x, nums)
print(list(output)) # [4, 9, 16]
Basic rule: map(func, iterable) → takes each item from iterable → runs it through func → yields each result
That’s the structure. It’s as close to functional programming as you get in base Python.
What about two lists?
Let’s add two lists together:
a = [10, 20, 30]
b = [1, 2, 3]
combined = map(lambda x, y: x + y, a, b)
print(list(combined)) # [11, 22, 33]
Element-by-element. Shorter list limits the range.
No loop indexes. No zip. Just direct application.
Trim that list — enter filter()
This one? Much simpler. Keep only what passes a test.
Imagine a list:
nums = [10, 13, 18, 21, 26]
Filter out odd numbers:
evens = filter(lambda n: n % 2 == 0, nums)
print(list(evens)) # [10, 18, 26]
Last point highlights the rule: filter(func, iterable)
Each value passes through func — if True, it’s included.
Try another:
words = ["egg", "", "carrot", " ", "beans"]
valid = filter(lambda x: x.strip(), words)
print(list(valid)) # ['egg', 'carrot', 'beans']
Whitespace-only strings get removed. Strip handles it.
That’s real. That comes up in messy datasets, API responses, free-form logs.
reduce(), the skippable-but-sometimes-so-useful part
You don’t need reduce unless you do.
It’s not a judgment call. It’s just the truth.
If you need to take a list and collapse it into one thing — a count, a max, a rollup sum — that’s when reduce happens.
But, first? You’ve got to grab it:
from functools import reduce
Everyone forgets that once.
Now, initialize:
nums = [1, 2, 3, 4]
total = reduce(lambda x, y: x + y, nums)
print(total) # 10
Step-by-step:
- x=1, y=2 → 3
- x=3, y=3 → 6
- x=6, y=4 → 10
Want a product?
nums = [2, 4, 6]
product = reduce(lambda x, y: x * y, nums)
print(product) # 48
Now here’s a classic:
vals = [66, 89, 72, 91]
high = reduce(lambda a, b: a if a > b else b, vals)
print(high) # 91
You could just use max(). But later, throw in complex logic? That lambda pays off.
Chaining: map() + filter() + reduce()
You’ll want to combine these. That’s not “over-use” — it’s actual flow.
Let’s process a list of numbers:
- Square each number
- Keep only if it’s even
- Sum everything left
from functools import reduce
nums = [1, 2, 3, 4, 5]
step1 = map(lambda x: x * x, nums) # [1, 4, 9, 16, 25]
step2 = filter(lambda x: x % 2 == 0, step1) # [4, 16]
result = reduce(lambda x, y: x + y, step2)
print(result) # 20
It’s noisy as a one-liner. But in steps? It’s readable. Traceable. You can insert debug when needed. Maybe even switch the lambdas into named functions.
Objectively: not everything needs to be composed. But when it does? Compose clearly.
Using them to clean raw data (real-world)
You’ve got scraped strings from somewhere:
raw = ["3", "42", "", " ", "error", "7", "12"]
Goal:
- Strip blanks
- Keep only pure digits
- Convert to int
- Sum all values
Try this:
from functools import reduce
cleaned = filter(lambda x: x.strip().isdigit(), raw)
numbers = map(lambda x: int(x.strip()), cleaned)
total = reduce(lambda a, b: a + b, numbers)
print(total) # 64
Transform-filter-reduce. Readable. Minimal. Functional.
That’s what they’re designed for.
They’re lazy
map() and filter() won’t act until you iterate through the output.
Example:
out = map(lambda x: x * 10, range(5))
print(out) # <map object at ...>
print(list(out)) # [0, 10, 20, 30, 40]
print(list(out)) # []
Because once you consume the iterator, it’s gone.
This laziness cuts memory usage, which is good. But surprises folks expecting arrays.
Always wrap list() around testing. Debugging, too.
Wrapping up
When is map() better than a loop? When the function is short. When purpose is clear. When is filter() better? When condition is simple. Strip junk. Remove edge cases. When to reduce()? When you need a value out of a list — and you define how.
Avoid chaining for fun. Do it when steps are clean. If they aren’t, write functions. Break it up.
map-filter-reduce — functional ideas, just done small in Python.
Done right, they’re useful. Nothing more. Nothing less.
✨ Thanks for reading! I’d love to hear your thoughts — drop a comment below and let’s keep the conversation going.
Stay connected with me here:
- 🎯 Topmate: topmate.io/yash0307jain
- 🔗 LinkedIn: linkedin.com/in/yash0307jain
- 💻 GitHub: github.com/yash0307jain
- 🌟 AlgoMart (Follow for exciting projects!): github.com/AlgoMart
Until next time — let’s keep creating, sharing, and growing together. 👋
메타데이터
- post_id
- 21c3f06e6e4b
- slug
- functional-programming-with-map-filter-and-reduce-in-python-21c3f06e6e4b
- url
- https://medium.com/algomart/functional-programming-with-map-filter-and-reduce-in-python-21c3f06e6e4b
- canonical_url
- https://medium.com/algomart/functional-programming-with-map-filter-and-reduce-in-python-21c3f06e6e4b
- author_url
- https://medium.com/@yashjainio
- status
- ok
- fetched_at
- 2026-08-18 01:51:17