Built-In Functions In Python - Map() And Reduce()

Map()

 
Map() is a built-in function in Python. It takes two arguments- the function and the sequence, as shown below.
 
Syntax: map (function, sequence1 [, sequence2,...])
 
and then, it returns a list of results after applying the given function to each item of the sequence.
 
Illustration 1
 
For example, we created a function farenheit(T), which is capable of converting the temperature from degree centigrade to Fahrenheit as shown below.
 
Built-In Functions In Python - Map() And Reduce() 
 
But suppose, we want the temperature in Fahrenheit for a list of temperatures in degree centigrade. In such cases, map() comes into play. We create a list of temperatures named temp.
 
Built-In Functions In Python - Map() And Reduce() 
 
Using map() with Lambda Expressions
 
The map() function is very useful especially when it is used in conjunction with the lambda expression.
 
In this way, we do not need to create a separate function.
 
Built-In Functions In Python - Map() And Reduce() 
 
Passing more than one sequence iterable
 
The map() has the capability to handle more than one iterables.
 
Built-In Functions In Python - Map() And Reduce() 
 
Clearly, the function takes in the sequence iterables in order and processes each member of the list at a time.
 
Illustration 2
 
Built-In Functions In Python - Map() And Reduce() 
 
Thus, the idea of map() is pretty simple, you put in a function name (without parenthesis), then you pass in the sequence iterable like a list. It then returns a list of that function applied to all the elements of that sequence.
 

Reduce()

 
The reduce () function, just like the map() takes in two arguments- the function and the sequence. However, the difference lies in the method in which the reduce() executes those sequence iterables. Unlike map() that maps every element to the function, reduce() continually applies the function to the sequence.
 
How does reduce() work?
 
Now suppose we have the sequence seq as,
 
seq = [s1, s2, s3]
and the reduce function as:
reduce (function, seq)
 
Step 1
 
The first two elements will be applied to the function, i.e., function(s1, s2)
 
Step 2
 
The list on which reduce() works now looks like this.
 
[function(s1, s2), s3]
 
Step 3
 
The function will now be applied to the result of the previous and the last element of the sequence,
 
[function(function(s1, s2), s3)]
 
Please note that for n elements in the sequence, this process continues until only one element is left and thereby returning that one element as the result of the reduce().
 
Illustration
 
Built-In Functions In Python - Map() And Reduce() 
 
Explanation
 
In the above illustration, we defined a list of numbers whose sum of elements needs to be calculated.
 
The lambda function defined inside the reduce() takes in 2 elements of the list and calculates their sum. It further sums the result with the next element of the list. This iteration continues until there is only one element left i.e. the final sum of all the elements in the list. The reduce() then returns this as the result, in this case, it is 109.


Similar Articles