Learn how to use and the main differences between the Map Filter Reduce Python built-in functions with some great examples!
Hello dear reader! Hope you are doing well. In this short code snippet, we will learn how to use the Map, Filter & Reduce python functions along with some examples.
These 3 functions come built-in into the language, meaning you don’t have to import them from any external library, and are highly useful when working with arrays or iterable objects. The 3 of them are high-order functions, meaning that as their arguments they take in another function and a iterable on which to evaluate such function.
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
The Python map( ) function takes an array and applies another function to it. In other words, to help you remember the objective of the function, it maps an input array to an output array defined by another function.
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?
Notice how we had to cast the map(double, input_array) into a list, as being a higher order function the map python function returns a function and not an actual array value.
No products found.
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.
Easy peasy lemon squeezy!
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!