Understand the function of Lambda, Filter, Map, Reduce in Python with Examples
Have you ever got frustrated by using regular loops and functions in python and have wondered if there are crisp and efficient ways to code? Then, this blog is for you. This blog gives you a brief understanding of lambda, filter, map, and reduce functions in Python. We’ll be covering these topics with examples to make you familiarize with these concepts. So, Let’s get started!!!
Lambda()
Lambda is a small anonymous micro-defined function that can take any number of arguments but execute only one expression. It can also be used for returning object functions.
Example
lamb = lambda x , y , z: x * y + zprint("For x=5, y=4 and z=7 : "+str(lamb(5,4,15)))
Output
For x=5, y=4 and z=7 : 35
In the above example, ‘lamb’ is an identifier having the lambda function with x, y, and z as their variables. lambda function can return the object function without using the return argument. which is assigned to the identifier ‘lamb’.
Map()
Map is a function that contains a user-defined function and an iterable iterator. When the map function is called, every element present in the iterator is passed separately inside the user-defined function and stores the elements returned as an iterator.
Example
def cube(x): return x*3lst=[1,2,3,4,5]result=map(cube,lst)print("Result = "+str(list(result)))
Output
Result = [3, 6, 9, 12, 15]
Here, the function map is calling a user-defined function “cube” which intakes only 1 variable and passes every element in the iterator ‘lst’ is passed one by one to the user-defined function cube. When the user-defined function ‘cube’ is returning an object, then ‘result’ stores them as an iterable object.
Reduce()
The function ‘reduce’ needs to be imported from ‘itertools’ and contains a higher-order function. Reduce contains a user-defined function and an iterable sequence, but unlike a map, it finally gains only one output as a result.
Example
from functools import reducedef sum(x,y):return x+y#sum=lambda x,y:x+y#This also gives same output as def sum function but by using lambdalst=[1,2,3,4,5]result = reduce(sum,lst) #1+2=3#3+3=6#6+4=1010+5=15#print("Result = "str(result))
Output
Result = 15
Filter()
The FIlter function is similar to for loop but faster than it. It passes an iterable object in a user-defined function and returns the values which are satisfied by the conditions.
Example
check=lambda n:n%2==0lst=[1,2,3,4,5]result=filter(check,lst)map_result=map(check,lst)print(‘Map function of’+str(lst)+’: ‘+str(list(map_result)))print(“After using filter function: “+str(list(result)))
Output
Map function of[1, 2, 3, 4, 5]: [False, True, False, True, False]After using filter function: [2, 4]
If you are interested to know the basics of Machine Learning, Check out my other blog ‘What is Machine Learning? — An Introduction to Machine Learning’.
Feel free to reach out to me anytime if you want to discuss something then connect with me through Linkedin, Github and Facebook