Learn how Python’s map, filter, and functools.reduce work, what they return, and when a comprehension is easier to read.
Python map, filter, and reduce: the difference
map transforms each item, filter keeps items that pass a condition, and reduce combines items into one accumulated result. In Python 3, map and filter return lazy iterators; reduce is imported from functools.
Choose the clearest expression: use a list comprehension for a simple transformation or filter, and use sum, min, or max for familiar aggregations. Reach for reduce when a custom accumulation is clearer than those alternatives.
Practise by doubling a list, keeping its even values, and summing the result. Then compare your solution with the examples below. Our Python practice guide suggests a routine for building fluency.
Turn the concepts into a working ML project. Read the free Starter Kit excerpt on test sets to preview the €19 handbook. No email required.
map and filter are built-ins. reduce belongs to the standard-library functools module, so import it before use. All three accept a callable and an iterable; reduce also accepts an optional initial value.
Although this can sound a bit confusing map, filter and reduce in Python are very useful and easy to understand functions when see along with examples, so lets get to it!
We will look at each of them individually and then comment their differences.
Map Filter and Reduce in Python with examples
Python map function

map(function, iterable) returns an iterator that applies the function as you consume items. It accepts general iterables, not just arrays.
def double(x):
return x*2
input_array = [1,2,3,4]
output_array = list(map(double,input_array))
#output_array = [2,4,6,8]
In the previous example our map function turned the input array into the output array using the previously defined double function. Easy right?
list(map(double, input_array)) materializes the iterator into a list. map returns an iterator, not a function. Once an iterator is exhausted, iterating over it again yields no further items.
Practice tip: rewrite the example as [double(x) for x in input_array]. Compare readability before choosing a style; neither form is automatically the best for every task.
Python Reduce function

The Python reduce( ) function takes an array and applies an aggregation function to it in order to return a single value. In other words, to help you remember the objective of the function, it reduces an input array to single value defined by another function.
from functools import reduce
def add(a,b):
return a+b
input_array = [10,10,10,10]
output = reduce(add, input_array)
#output = 40
In the previous example we used the python reduce function to add the values in the input array, resulting in a single integer output.
For addition, sum(input_array) is simpler than reduce. If your iterable may be empty, provide an initial value such as reduce(add, input_array, 0); without one, reduce raises TypeError on an empty iterable.

Lets get to our last function, the filter functon!
Python Filter Function

The Python filter( ) function takes an array filters its values according to a condition defined by another function. This means that given an initial input array, it returns those elements of this initial array that meet a certain criteria. Lets look at this with an example:
def even(x):
return x%2 == 0
input_array = [1,2,3,4]
output_array = list(filter(even,input_array))
#output_array = [2,4]
In the previous example we can see how we used the filter python function to return only the even elements of the initial input array.
Conclusion
In this post we have quickly learned about the Map, Reduce, and Filter Python functions. You should not confuse them with the Mapreduce or Map from the Spark framework, which is something different.
Filter, Map and Reduce are very much used with Pythons anonymous or lambda functions.
For further resources on learning Python, check out the following list:
Also, some similar books (introductory Python books) to this one are:
- Automate the Boring Stuff with Python by Al Sweigart.
- Think Python by Allen B. Downey.
- Learning Python by Mark Lutz.
More advanced books, which will get you absolutely rocking Python are:
- Fluent Python by Luciano Ramalho.
- The Hitchikers Guide to Python.
- Architecture Patterns with Python by Harry J.W Percival and Bob Gregory.
Additional online resources to learn elegant and awesome Python are:
- PEP8 Python Style guide.
- Kaggle Learn Python Tutorials.
- Real Python programming guides.
- Code Wars: code code and code.
- DataCamp Python Courses.
Enjoy them, and thanks for reading How to Learn Machine Learning!
Tags: Map Reduce Python, Python Map Reduce, Python Map Filtr, Filter Map Reduce in Python.
Subscribe to our awesome newsletter to get the best content on your journey to learn Machine Learning, including some exclusive free goodies!



