Lambda Functions in Python
Lambda Functions in Python
Lambda functions in Python are those function that have no known because of which they are also called anonymous functions in Python. They are declared using the lambda keyword instead of the keyword def.
Create Lambda Functions in Python
Lambda functions are created by using a keyword lambda followed by the names of parameters it will be using and then a line of statement. It is important to note that lambda function can have any number of parameters but only one line as function definition.
Syntax:
# Lambda function definition lambda parameters: statement
Example:
# Python program to exemplify lambda function creation product = lambda num1, num2 : num1 * num2 print(product(2, 5))
In the code provided above, a lambda function is declared with 2 parameters. Those parameters are multiplied together to return the their product.
Output:
10
Use of Lambda Function
Uses of Lambda function are as follows:
- It can be used anonymously inside another function
- It can be used for different scenarios with same function definition
- It can be used as argument for those built-in functions of Python that require functions as parameters instead of creating a function using def keyword
Example:
# Python program to exemplify use of lambda function def area(constant): return lambda a, b=1 : constant * a * (b if b != 1 else a) circle_area = area(22/7) square_area = area(1) rectangle_area = area(1) print(circle_area(7)) print(square_area(4)) print(rectangle_area(5, 8))
In the code provided above, a lambda function has been defined in the return statement of another function. The function is then called with an argument for a constant if any otherwise with 1. Function definition contains only a single return statement which returns definition of the lambda function to where it is called. Then the lambda function is called with values of the sides of the figure of which are is being calculated.
Output:
154.0 16 40
Lambda Function with Built-in Functions
There are a number of built-in functions in Python that require functions as parameters. So, for these functions, anonymous or more popularly known as lambda functions can be used. Few of those built-in functions are map(), reduce(), filter() etc.
Example: Lambda Function with map() function
# Python program to exemplify use of lambda function with map function List = [1, 2, 3, 4, 5] print(List) newList = list(map(lambda i: i**2, List)) print(newList)
In the code provided above, a lambda function is declared with 1 parameter. It is passed as an argument to the map() function along with a list. Purpose of the above lambda function is to find the square of everly list element and returns those squares as a list.
Output:
[1, 2, 3, 4, 5] [1, 4, 9, 16, 25]
Example: Lambda Function with filter() function
# Python program to exemplify use of lambda function with filter function List = list(filter(lambda i: (i%2!=0) and (i%3!=0), range(1,21))) print(List)
In the code provided above, a lambda function is declared with 1 parameter. It is passed as an argument to the filter() function along with a range. Purpose of the above lambda function is to filter out the multiple of 2 or 3 from the range and return a list.
Output:
[1, 5, 7, 11, 13, 17, 19]
Example: Lambda Function with reduce() function
reduce() function must be imported from the module functools before using it in the program.
# Python program to exemplify use of lambda function with filter function from functools import reduce Set = {10, 20, 30, 40, 50, 60} max_num = reduce(lambda num1, num2: num1 if num1 > num2 else num2, Set) print(max_num)
In the code provided above, a lambda function is declared with 2 parameters. It is passed as an argument to the reduce() function along with a set. Purpose of the above lambda function is to reduce the set until it has only a single element which is the maximum of all the elements of the set.
Output:
60