The concept of lambda functions/anonymous functions is one of the interesting topics in Python. It seems like it’s the tough topic but you feel easy during the practice.
The functions which are defined anonymously (without name) are called as anonymous functions. Here we use a keyword ‘lambda’ .Therefore anonymous functions are otherwise called as lambda functions.
The only difference between normal functions and anonymous functions is keywords (def and lambda). We define normal functions by using a keyword “def” keyword whereas anonymous functions are defined using the lambda keyword.
Syntax of lambda function is:
|lambda [args1,args2…]: expression|
In Python, wherever you place lambda expression it returns the value of the expression.
|>>> add=lambda a:a+2 #Lambda function performing addition
>>> sub=lambda b:b-4 #Lambda function performing subtraction
>>> mul=lambda c:c*6 #Lambda function performing multiplication
>>> div=lambda d:d/7 #Lambda function performing division
>>> sub=lambda b:b-4
###Here the resultant is 8 because the expression lambda b:b-4 returns 2 and the statement print(add(6)) adds that resultant with 6 and returns the output 8.
Consider one of the above functions is ‘add=lambda a:a+2’. Here ‘a’ is an argument and a+2 is the expression which gets evaluated and returned.
The statement ‘add=lambda a:a+2’ is approximately similar as follows:
Declaration of lambda functions:
We can declare the lambda functions by the following ways.
Lambdas function with no parameters.
>>> val( )
>>> fun=lambda: True
>>> fun( )
>>> func=lambda: False
>>> func( )
>>> fun=lambda: print(“hello sudhakar”)
>>> fun( )
Lambdas function with one parameter.
|>>> x=lambda a:a+2
>>> f=lambda b:b*b
Lambdas function with two parameters
|>>> func=lambda s,t:s*t
Lambdas function with three parameters
|>>> L=lambda p,q,r:p+q-r
Passing string as an argument to the lambda function
We can also pass a string as an argument to the lambda function and is shown in below:
|>>> L_fun1=lambda s:print(s)
Lambdas with Built-in functions
We can use lambda functions with built-in functions such as filter ( ) and map( ).
The filter() function in Python takes in a function and a list as arguments. When the function evaluates to true it returns the new list otherwise not.
Ex: In the below example it takes all the items in the list. When we use filter( ) function it filters all the items which are divisible by ‘3’ and displays that newly generated list.
|#An example lambda function that prints the multiples of ‘3’.
>>> List1=list(filter(lambda x:(x%3==0),List))
[3, 6, 9]
#If the function evaluates to false it prints nothing. We have shown this for the above example.
>>> L1=list(filter(lambda x:(x%3==0),L))
It is similar to filter( ) function but instead of printing the resultant elements it prints true or false i.e map ( ) returns values of the expression for each element in the list. For the above same example we applied map( ) function.
>>> List1=list(map(lambda x:(x%3==0),List))
[False, False, True, False, False, True, False, False, True, False]
>>> L1=list(map(lambda x:(x%3==0),L))
[False, False, False, False]
The following example differentiates the way of using the normal functions and lambda functions.
#To find the highest number among two numbers using lambda functions.
|>>> highest=lambda x,y:print(“x is highest”) if x>y else print(“y is highest”)
y is highest
# The same above program can be written by using functions and is as follows:
1.A Python program which prints 4 powers of the numbers in the list by using map( ) function.
>>> list(map(lambda p:p**4,d))
[1, 16, 81, 256, 625]
2.A Python program that extracts only the values which are divisible by ‘10’ from the list by using filter( ) function.
>>> list(filter(lambda c:c%10==0,N))
[110, 40, 20, 80, 60]