Python Map, Filter & Reduce
Python provides many built-in functions that are predefined and can be used by the programmers by just calling them. These functions not only ease our work but also create a standard coding environment. In this tutorial, we will learn about three important functions: map(), filter, and reduce() in Python. These functions are most commonly used with Lambda function. "Lambda functions are functions that do have any name". These functions are used as parameters to other functions.
Why are lambdas relevant to map(), filter() and reduce()?
These three methods expect a function object as the first argument. This function object can be a normal or predefined function. Most of the time, functions passed to map(), filter(), and reduce() are the ones we only use once, so there's often no point in defining a named function(def function).To avoid defining a new function, we write an anonymous function/lambda function that we will only use once and never again.
"A map function executes certain instructions or functionality provided to it on every item of an iterable."
The iterable could be a list, tuple, set, etc. It is worth noting that the output in the case of the map is also an iterable i.e., a list. It is a built-in function, so no import statement required.
A map function takes two parameters:
First one is the function through which we want to pass the items/values of the iterable
The second one is the iterable itself
The map()function passes each element in the list to a lambda function and returns the mapped object.
"A filter function in Python tests a specific user-defined condition for a function and returns an iterable for the elements and values that satisfy the condition or, in other words, return true."
It is also a built-in function, so no need for an import statement. All the actions we perform using the filter can also be performed by using a for loop for iteration and if-else statements to check the conditions. We can also use a Boolean that could take note of true or false, but that would make the process very lengthy and complex. So, to simplify the code, we can use the filter function.
It also takes two parameters:
First one is the function for which the condition should satisfy
The second one is the iterable
"Reduce functions apply a function to every item of an iterable and gives back a single value as a resultant".
Unlike the previous two functions (Filter and Map), we have to import the reduce function from functools module using the statement:
from functools import reduce
We can also import the whole functools module by simply writing
But in the case of bigger projects, it is not good practice to import a whole module because of time restraint.
Like the previous two, it also takes two-parameter. First one is the function and the second one is the iterable
Its working is very interesting as it takes the first two elements of the iterable and performs the function on them and converts them into a single element. It proceeds further, taking another element and performing the function of that one and the new element. For example, if we have four digits and the function wants to multiply them, then we can first multiply the first two and then multiply the third one in their resultant and then the forth and so on. The reduce is in the functools in Python 3.0. It is more complex. It accepts an iterator to process, but it is not an iterator itself. It returns a single result.