Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 49

map, filter and reduce in Python

In this lesson: Use map(), filter() and reduce() in Python with lambda — clear examples, output and list-comprehension alternatives.

What are map, filter, reduce

map, filter and reduce are three tools for processing a list without writing full loops. map changes every item, filter keeps only some items, and reduce boils the whole list down to one value. They come from functional programming and make list work compact.

💡 map = transform all, filter = keep some, reduce = combine into one.

map — transform every item

nums = [1, 2, 3, 4]
doubled = map(lambda x: x * 2, nums)
print(list(doubled))
[2, 4, 6, 8]

map applies a function to every item and returns a new sequence. We wrap it in list() to see the values, because map returns a lazy iterator.

filter — select items

nums = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, nums)
print(list(evens))
[2, 4, 6]

filter keeps only the items for which the function returns True. Here it keeps even numbers. It can return fewer items than the original.

reduce — combine into one

from functools import reduce
nums = [1, 2, 3, 4]
total = reduce(lambda a, b: a + b, nums)
print(total)
10

reduce takes two items at a time and combines them, then combines the result with the next, and so on — folding the whole list into one value. Note it must be imported from functools.

Using lambda

A lambda is a tiny unnamed function, perfect to pass into these tools.

square = lambda x: x * x
print(square(5))          # 25
# same idea used inline:
print(list(map(lambda x: x * x, [1, 2, 3])))
25
[1, 4, 9]

lambda x: x * x is a one-line function. You can name it or drop it straight into map/filter/reduce.

map/filter vs comprehension

Often a list comprehension does the same job more readably.

# map:
list(map(lambda x: x * 2, nums))
# comprehension (clearer):
[x * 2 for x in nums]

For simple transforms and filters, a comprehension is usually the Pythonic choice. Use map/filter when you already have a named function to apply.

Common mistakes

  • Forgetting list(). map and filter return iterators — wrap them to see values.
  • Not importing reduce. It comes from functools, unlike map and filter.
  • Overusing lambda. For anything complex, a named def function is clearer.
  • Reusing an iterator. A map/filter result is consumed once; convert to a list to reuse.

Summary

  • map transforms every item into a new sequence.
  • filter keeps only items that pass a test.
  • reduce (from functools) combines all items into one value.
  • lambda gives a quick one-line function to pass in.
  • For simple cases, a list comprehension is often clearer.

Next, compare with list comprehension, or organise data with list, tuple, set and dict.

Frequently Asked Questions

What do map, filter and reduce do?
They process a list in three ways: map transforms every item, filter keeps only items that pass a test, and reduce combines all items into a single value. Together they cover most list-processing needs.
What is a lambda function?
A lambda is a small, one-line unnamed function, like lambda x: x * 2. It is handy to pass directly into map, filter or reduce without defining a full function with def.
Why does map return a map object, not a list?
For efficiency, map and filter return lazy iterators that produce values only when needed. Wrap the result in list() to see all the values at once.
Where does reduce come from?
Unlike map and filter, reduce is not built in — you import it from the functools module: from functools import reduce. Then it combines items pairwise into one result.
Should I use map/filter or a list comprehension?
For simple transforms and filters, a list comprehension is often clearer and more Pythonic. map and filter shine when you already have a named function to apply. Both are correct — choose readability.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicDeep Copy vs Shallow Copy Your progress: 49 / 66 (74%) · Lesson 50 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free