Lambda Expressions in Python
In this lesson, you will learn about lambda expressions in python, syntax along and how to use Lambda expressions with some example.
Lambda Expressions in Python
The lambda function is denoted by the lambda keyword and it is an anonymous function in Python. A lambda function can take an infinite number of arguments, but it is restricted to one expression.
Syntax:
lambda arguments : expression
Now let’s use the lambda functions in programs to understand more about them.
Sample program using lambda function in python,
Input:
[gamipress_button label=”Try It” onclick=”window.open(‘https://coderseditor.com/?id=1382′,’_blank’)”]
x ="Developer Publish Academy" (lambda x : print(x))(x)
Output:
Developer Publish Academy
Sample program using lambda function in Python
Input
[gamipress_button label=”Try It” onclick=”window.open(‘https://coderseditor.com/?id=1381′,’_blank’)”]
value = [lambda x=x: x*5 for x in range(57, 67)] for i in value: print(i())
Output:
285 290 295 300 305 310 315 320 325 330
Sample program using lambda function with multiple statements in python
Input:
[gamipress_button label=”Try It” onclick=”window.open(‘https://coderseditor.com/?id=1380′,’_blank’)”]
List = [[1,5,7],[7, 5, 9, 14],[57, 75, 67, 76]] sortList = lambda x: (sorted(i) for i in x) highest = lambda x, f : [y[len(y)-1] for y in f(x)] result = highest(List, sortList) print("Highest values in the list:",result)
Output:
Highest values in the list: [7, 14, 76]
Sample program using the lambda function with def function in python
Input:
[gamipress_button label=”Try It” onclick=”window.open(‘https://coderseditor.com/?id=1376′,’_blank’)”]
def lmabdafunction(x): return lambda a : a * x result = lmabdafunction(57) print(result(75))
Output:
4275