Lambda Function in Python

Lambda function in python is also called python anonymous function without formal defination of function. Formal defination of function means a function in python that is defined using def keyword and have some identifier. Lambda function is defined using lambda keyword. Lambda function can accept parameters and have a single return expression.

Python lambda syntax:

The syntax of lambda function in python is as:

lambda parameter_list : return expression

Where parameter list is the argument list separated by comma and return expression is the result or output generated by the lambda function.

Lambda expression python:

We can only specify a single expression in python lambda function. This expression may be of any valid arithmetic, logical or relational expression.

Python lambda example:

# lambda function to get square

# square will be used to call lambda function
square = lambda a : a * a

# calling lambda function
print(square(2))

Use of lambda in python:

Lambda function has multiple uses in python programming. It can be used in some built in functions such as map(), filter() that take function return to process data list.

Python map lambda:

Python lambda function can be provided to the map() function. This function will accept each value from the list and process it using function.

Example:

# Program to increase value by 1 map()

py_lst = [3 , 5 , 7 , 9 , 11 , 13]

map_lst = list(map(lambda x: x + 1 , py_lst))

print(map_lst)

Output:

[4, 6, 8, 10, 12, 14]

Python filter lambda:

Another important and common use of lambda function is with python filter() function. We can use it in filter() function to filter values from list according to the provided expression.

Example:

# Program to print only leap years using
# filter and lambda function

lst_years = [2003 , 2004 , 2007 , 2008 , 2020 , 2022]

lst_leap_years = list(filter(lambda x: (x % 4 == 0) , lst_years))

print(lst_leap_years)

Output:

[2004, 2008, 2020]

Python sort lambda:

Python lambda function can also be used with sort() function to sort list according to the specific key value.

Example:

# Program to sort list using
# psecific key with lambda function

mylst = [('Laptop' , '70' , '500') ,
       ('Mouse' , '30' , '700') ,
       ('Printer' , '10' , '400')]
# It will sort list using second key
mylst.sort(key=lambda x:x[1])

print(mylst)

Output:

[('Printer', '20', '300'), ('Laptop', '30', '100'), ('Mouse', '50', '200')]

Lambda handler:

Normally lambda function is used inside other standard functions to process data quickly. In python we can also assign some handler to lambda function. This is valid identifier and used as Lvalue of the lambda function such as.

lambda_handler = lambda parameter_list : return expression

Python lambda multiple arguments:

We can also pass mmultiple arguments to the lambda function separated by comma. But there will only single expression to process these all arguments.

Example:

# Program to add two numbers
# using lambda function

add = lambda a , b : a + b

# call to lambda function
# using lambda handler

print(add(2,5))

Output:

7
 
Comments
Login to TRACK of Comments.