Python Tutorial 33 --- "Map, Filter & Reduce"
Python Tutorial 33 --- "Map, Filter & Reduce"
Figure 1:Anonymous/Lambda Functions In Python
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.
map():
"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.
SYNTAX:
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
Example:
The map()function passes each element in the list to a lambda function and returns the mapped object.
filter():-
"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.
SYNTAX:
It also takes two parameters:
- First one is the function for which the condition should satisfy
- The second one is the iterable
Example:
reduce():
"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
Import functools
But in the case of bigger projects, it is not good practice to import a whole module because of time restraint.
SYNTAX:
Example:
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.
CODE-
# Map, Filter & reduce
#---------- MAP ----------
# list1 = ["5", "10", "15", "20", "25"]
#
# # Normal Method (for loop)
# # for i in range(len(list1)):
# # list1[i] = int(list1[i])
#
# # Map Function
# list1 = list(map(int, list1))
#
# list1[2] = list1[2] + 1
# print(list1[2])
#
#
# # def sq(a):
# # return a * a
# #
# # list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# # list2 = list(map(sq, list2))
# # print(list2)
#
# # Using lambda function with map
# list2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# list2 = list(map(lambda x: x*x, list2))
# print(list2)
# def square(a):
# return a*a
#
# def cube(a):
# return a*a*a
#
# func = [square, cube]
# for i in range(11):
# val = list(map(lambda x:x(i), func))
# print(val)
#---------- FILTER ----------
# list3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#
#
# def is_greater5(num):
# return num > 5
#
#
# gr_than5 = list(filter(is_greater5, list3))
# print(gr_than5)
#---------- REDUCE ----------
from functools import reduce
list4 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Normal method
# num = 0
# for i in list4:
# num = num+i
# print(num)
# Reduce method
num = reduce(lambda x,y:x+y, list4)
print(num)
Comments
Post a Comment