Python lambda() Function
In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python.
Python lambda functions are anonymous functions. It means that a function is without a name. Any function in python is defined using the ‘def’ keyword. Similarly, a lambda function is defined using a ‘lambda’ keyword for defining the anonymous functions.
The syntax for creating a lambda function is:
lambda arguments: expression
- Python lambda function can have any number of arguments, but it will only have one expression. That expression is later on evaluated and returned.
- Anyone can use the lambda function whenever an object function is required.
- We should always remember that lambda functions are syntactically restricted to one expression only.
Example of Lambda Function
Let’s understand with an example of the lambda function.
# Program to show the use of lambda functions double = lambda x: x * 10 print(double(2))
In the above program, we have used the lambda function using the lambda keyword and an expression. lambda x: x*10 is a lambda function. In this lambda function, x is the argument, and x*10 is an expression. This expression is evaluated and returned. The function has no name and returns a function object assigned to the identifier double. We can call it a normal function.
double = lambda x: x * 10
can also be written as
def double(x): return x * 10;
Difference between Lambda function and def function
Using an example, let’s understand the difference between the lambda function and a def function. This program will return the square of a number.
def square(y): return y*y lambda_square = lambda y: y*y # using the normally # defined function print(square(5)) # using the lambda function print(lambda_square(5))
As you can see, both functions square() and lambda_square()are doing the same thing. They both give an output of the square of the numbers passed as argument. When we use a function, we have to pass a value to the function defined using the def keyword. In this function, we have to give a name to the function. After evaluating the value, we can return the value to where the function was called using the return keyword.
While we use the lambda function, we don’t have to use the return statement. It always returns an evaluated and returned expression without using any return keyword. We can also put the lambda expression anywhere in the code, and we don’t have to assign it to a variable. This is how we can use a lambda function.
Using of Lambda function with list comprehension
y = [lambda x=x: x*2 for x in range(1, 11)] for table in y: print(table())
2 4 6 8 10 12 14 16 18 20
In the above code, we have used the lambda function with a list comprehension. This program will print the table of two from range 1 to 10. In this program, we have used x as an argument and x*2 as an expression. This expression will get evaluated and returned, and get printed.
Use of lambda function
We use lambda functions mostly when we want a nameless function for a short period. In python, lambda functions are used as an argument for other higher-order functions. Higher-order functions are those functions that take another function as an argument. There are two built-in functions along with which we use lambda functions: map () and filter().
Use with filter( )
# Program to filter out only the odd items from a list list_val = [1, 5, 4, 6, 8, 11, 3, 12] list1 = list(filter(lambda x: (x%2 != 0) , list_val)) print(list1)
[1, 5, 11, 3]
We used the filter() function in the above code to return the odd numbers from the list. This function takes the lambda function as an argument and the list. After evaluating the expression (x%2 !=0), we will return the value and print the list.
Use of map()
# Program to double each item in a list using map() list1 = [1, 5, 4, 6, 8, 11, 3, 12] my_list = list(map(lambda x: x * 2 , list1)) print(my_list)
[2, 10, 8, 12, 16, 22, 6, 24]
In the above code, we have the map() function to double all the elements of the list. In the map() function, we have passed the lambda function and the list as an argument, and after that, when the expression( x*2) is evaluated, we return the value.