Introduction To Lambda Function In Python
In Python, there are three different types of function user-defined function, build-in function, an anonymous function. The anonymous function in python is declared by using the lambda keyword. Lambda function accepts any number of arguments in function but they return only one value in the expression.
Why we use a lambda function?
The lambda function in python is better shows when you use them as an anonymous function inside another function.
Syntax of anonymous function in python is given below.
lambda [argument 1,argument 2,..] : [ expression ]
Example 1:- create an anonymous function without a given name.
Lambda X, Y: X + Y Output:- <function __main__.<lambda>(X, Y)> Example 2:- Create an anonymous function with the name Code:- Power = lambda X : X ** 2 # Create Function with name Power( 5 ) # Call function using Function name and pass an argument. Output:- 25
Difference between user define function and anonymous function.
User Define Function
To create a user define function in python use def keyword and pass the function name then write the body of code.
Def Function Name( argument 1, argument 2): body return value Example:- create function to return the value multiply by two. Code:- def times2( a ): # Create Function return a * 2 # return value is multiply by two times2( 2 ) # call Function using function name times2 Output:- 4
Example:- create a function to return the value multiply by two.
Times2 = lambda a : a * 2 Times2( 2 ) Output:- 4
n the above example, we use user to define the function and anonymous function i.e lambda function in python, the difference is when you want to return a single expression on any argument then always use an anonymous function.
Use anonymous function in user define function.
Definition a function that takes one argument, and that argument will be added with an unknown number:
def fn_name(n): # Define function pass one argument return lambda a : a + n # Add unknown number and return expression value Add = fn_name(4) # the entered number is passed into the function fn_name. Add will contain a lambda function which is add argument in expression. Add( 8 ) # pass arguments to add an unknown number Output:- 12
In this example, we add 4 as the unknown number and then call the function pass arguments to add unknown value.
#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 addion of n is returned at each function call b = table(11) #the 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(0,11): print(n,"+",i,"=",b(i)) #the lambda function b is called with the iteration variable i. Output:- 11 + 0 = 11 11 + 1 = 12 11 + 2 = 13 11 + 3 = 14 11 + 4 = 15 11 + 5 = 16 11 + 6 = 17 11 + 7 = 18 11 + 8 = 19 11 + 9 = 20
Anonymous function with Filter and Map function
Map Function:- map function is used to pass number of arguments in function at same time and create new list
Syntex:- map ( Function , interables )
Example 1:- Use user define function pass given list into the function to perform some operations. Define function is return the power of number.
# Define Function def pow(var): # Create Function and pass single argument return var ** 2 Sequence = [1, 2, 3, 4, 5 ] # Apply Map Function list(map(pow,Sequence )) # Apply Map Function Pass Function name and interables and to create list pass map function into it. Output:- [1, 4, 9, 16, 25]
Example 2:- Use Anonymous function pass given list into the function to perform some operations. Define function is to return the power of the number.
Sequence = [1, 2, 3, 4, 5 ] list(map( lambda X : X ** 2 ,Sequence )) # Apply anonymous function directly to the map function. Output:-[1, 4, 9, 16, 25] It iterates Sequence value to the argument of lambada function and return a new list Example 3:- identify the number in the list is divided by 3 or not use comparison operator apply map function. my_list = [1,2,3,4,5,6] list(map(lambda a :(a/3 != 2),my_list)) # Pass condtion into lambda expression if value divide by 3 is not equal to 2 Output:- [True, True, True, True, True, False] Filter Function:- the filter function given the sequence with the help of function that tests each element in the sequence to be true or not if it is true then create list of the true numbers. Example:- Check the number in list is divided by 2 or not if it is true then create new list. Code:- seq = [1,2,3,4,5] # Create List list(filter(lambda item: item%2 == 0,seq)) # pas lambda function into filter function to chek that number in seq is divided by 2 of not # To show the output of new list use list() Output:- [2, 4] Example 2:- Return the value in the sequence is greater than 18. Code:- ages = [5, 12, 17, 18, 24, 32] list(filter(lambda a : a>=18,ages)) # Create list Values Greter than equal 18 using lambda function Output:- [18, 24, 32]
In this blog, you will get a better understanding of anonymous function in python and its syntax also give how to apply it in the user define function and build in function.