Learn Python – Python Lambda Functions- Basic and advance

Python Lambda characteristic is regarded as the nameless characteristic that is described except a name. Python approves us to no longer declare the function in the trendy manner, i.e., via the usage of the def keyword. Rather, the nameless functions are declared by way of using the lambda keyword. However, Lambda features can accept any quantity of arguments, however they can return only one value in the form of expression.

The nameless feature includes a small piece of code. It simulates inline features of C and C++, but it is no longer precisely an inline function.

The syntax to define an anonymous characteristic is given below.


lambda arguments: expression       

It can receive any quantity of arguments and has solely one expression. It is beneficial when the characteristic objects are required.

Consider the following example of the lambda function.

Example 1

# a is an argument and a+10 is an expression which got evaluated and returned.    
x = lambda a:a+10   
# Here we are printing the function object  
print("sum = ",x(20))  


<function <lambda> at 0x0000019E285D16A8>
sum =  30

In the above example, we have defined the lambda a: a+10 nameless characteristic where a is an argument and a+10 is an expression. The given expression receives evaluated and again the result. The above lambda characteristic is identical as the everyday function.

def x(a):  
    return a+10  
print(sum = x(10))  

Example 2

Multiple arguments to Lambda function

# a and b are the arguments and a*b is the expression which gets evaluated and returned.    
x = lambda a,b: a*b  
print("mul = ", x(20,10))  
<div class="codeblock3"><pre>  
mul =  200  
<h2 class="h2">Why use lambda function?</h2>  
<p>The main role of the lambda function is better described in the scenarios when we use them anonymously inside another function. In Python, the lambda function can be used as an argument to the <strong>higher-order functions</strong> which accepts other functions as arguments.</p>  
<p>Consider the following example:</p>  
<h3 class="h3">Example 1:</h3>  
<div class="codeblock"><textarea name="code" class="python">  
#the function table(n) prints the table of n    
def table(n):    
    return lambda a:a*n # a will contain the iteration variable i and a multiple of n is returned at each function call    
n = int(input("Enter the number:"))    
b = table(n) #the entered number is passed into the function table. b will contain a lambda function which is called again and again with the iteration variable i    
for i in range(1,11):    
    print(n,"X",i,"=",b(i)) #the lambda function b is called with the iteration variable i  


Enter the number:10
10 X 1 = 10 
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100

The lambda function is often used with Python built-in functions filter() characteristic and map() function.

Use lambda function with filter()

The Python built-in filter() function accepts a feature and a list as an argument. It presents an superb way to filter out all elements of the sequence. It returns the new sequence in which the feature evaluates to True.

Consider the following instance where we filter out the solely odd wide variety from the given list.

#program to filter out the tuple which contains odd numbers    
lst = (10,22,37,41,100,123,29)  
oddlist = tuple(filter(lambda x:(x%3 == 0),lst)) # the tuple contains all the items of the tuple for which the lambda function evaluates to true    


(37, 41, 123, 29)

Using lambda function with map()

The map() function in Python accepts a function and a list. It offers a new list which includes all modified items lower back by using the feature for each item.

Consider the following example of map() function.

#program to filter out the list which contains odd numbers    
lst = (10,20,30,40,50,60)  
square_list = list(map(lambda x:x**2,lst)) # the tuple contains all the items of the list for which the lambda function evaluates to true    


(100, 400, 900, 1600, 2500, 3600)