Map, Filter, and Reduce in Python With Real-World Examples
Learn how to use Python's map, filter, and reduce functions through practical scenarios like temperature conversion, filtering sale prices, and summing transactions. Understand when these tools beat list comprehensions for performance and clarity.
Here is the article you requested.
Map, Filter, and Reduce in Python: Real-World Examples You Can Use Today
If you have been writing Python for a while, you have probably seen code that loops through a list to change every item, or filters out unwanted values, or tries to combine a list into a single result. These tasks are so common that Python has three built-in functions made specifically for them: map, filter, and reduce.
While list comprehensions have become the popular choice for many developers, understanding map, filter, and reduce is still valuable. They give you a functional programming style that sometimes makes your code cleaner and more direct. And when you work with large datasets or need to write parallel code, these functions can be more efficient than loops.
Let me walk you through each one with examples you can actually use in your daily work.
map — Apply a Function to Every Item
The map function takes a function and an iterable, then returns a new iterator with the function applied to each item. It is straightforward and often faster than a manual loop.
Here is a real scenario. Suppose you work at a company that processes user data. You have a list of temperatures in Celsius from different sensors, and you need to convert them all to Fahrenheit.
celsius_temps = [0, 22, 35, 100, -10]
def to_fahrenheit(c):
return (c * 9/5) + 32
fahrenheit_temps = list(map(to_fahrenheit, celsius_temps))
print(fahrenheit_temps) # [32.0, 71.6, 95.0, 212.0, 14.0]
Without map, you would write a for loop and append to a new list. With map, the intention is clear: apply this conversion to everything.
You can also use map with multiple iterables. For example, if you have two lists and want to add them element-wise:
list_a = [1, 2, 3]
list_b = [4, 5, 6]
result = list(map(lambda x, y: x + y, list_a, list_b))
print(result) # [5, 7, 9]
At PythonSkillset, we often use map when cleaning data from CSV files — like trimming whitespace from every string in a list of headers.
filter — Keep Only What You Need
filter does exactly what its name suggests. You give it a function that returns True or False, and it returns an iterator with only the items that passed the test.
Imagine you are building a dashboard for an e-commerce site. You have a list of product prices, and you want to show only the items that are currently on sale (price under $20). Here is how you would do it:
prices = [45, 12, 8, 99, 15, 22, 3]
def is_on_sale(price):
return price < 20
sale_prices = list(filter(is_on_sale, prices))
print(sale_prices) # [12, 8, 15, 3]
This is much cleaner than a for loop with an if condition and a .append(). And because filter returns a lazy iterator, it can be memory efficient with large datasets.
You can also combine filter with lambda for quick, one-time logic:
even_numbers = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6]))
print(even_numbers) # [2, 4, 6]
When you deal with user input, filter is great for removing empty strings or None values from a list before processing.
reduce — Combine Items into a Single Value
reduce is different from map and filter. It does not return an iterable — it returns a single accumulated result. To use it, you need to import it from functools (in Python 3 it was moved out of built-ins).
The idea is simple: you have a list, and you want to combine all items using a function that takes two arguments. For example, finding the total of all numbers in a list:
from functools import reduce
numbers = [10, 20, 30, 40]
total = reduce(lambda a, b: a + b, numbers)
print(total) # 100
Here is how it works step-by-step: 1. It takes the first two items (10 and 20) and applies the lambda: 10 + 20 = 30. 2. Then it takes that result (30) and the next item (30): 30 + 30 = 60. 3. Then 60 + 40 = 100.
You can also provide an initial value as a third argument, which is useful when the list might be empty.
total_with_start = reduce(lambda a, b: a + b, numbers, 100)
print(total_with_start) # 200
reduce shines in more complex scenarios. At PythonSkillset, a common use case is computing the product of all numbers in a list for statistical calculations, or flattening a list of lists:
list_of_lists = [[1, 2], [3, 4], [5]]
flat = reduce(lambda acc, sublist: acc + sublist, list_of_lists, [])
print(flat) # [1, 2, 3, 4, 5]
When to Use Them vs. List Comprehensions
You might be wondering: should I use map/filter or list comprehensions? The honest answer is that list comprehensions are often more readable to most Python developers. But there are clear cases where the functional tools are better:
- Performance with large data:
mapandfilterreturn iterators, not lists. Plus, they run at C speed internally, which can be faster than a Python loop. - Code clarity for transformations: When you already have a named function (like
to_fahrenheit), usingmapreads better than a comprehension. - Parallel processing: You can easily combine
mapandfilterwithmultiprocessing.Poolfor parallel execution.
On the flip side, if you need complex logic inside the loop, a list comprehension or a regular for loop is usually clearer.
Putting It All Together
Here is a realistic pipeline. Suppose you have a list of transaction amounts (some may be negative or zero), and you want to: 1. Remove any non-positive values. 2. Apply a 10% tax to each. 3. Sum the total.
from functools import reduce
transactions = [250, -50, 100, 0, 75, -20]
positive = filter(lambda x: x > 0, transactions)
after_tax = map(lambda x: x * 1.10, positive)
total_collected = reduce(lambda a, b: a + b, after_tax)
print(f"Total collected: ${total_collected:.2f}") # Total collected: $467.50
Each step is clean and the chain is easy to read. You could also write this as a single comprehension, but the function-based approach separates the logic into distinct stages.
The Takeaway
Do not feel pressured to use map, filter, and reduce everywhere. But knowing them gives you another tool in your Python toolbox. When you encounter a situation where they fit naturally, they can make your code more efficient and expressive.
Next time you are processing a list, ask yourself: would map or filter make this cleaner? Would reduce help me collapse this data? If yes, go ahead and use them. Your future self — and anyone reading your code — will appreciate the clarity.
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.