Lambda function is like anonymous function is other programming languages. It's basically a inline function that has no name. You can say function oneliner 😎
add = lambda x, y: x + yprint(add(5, 2))
The example above takes two arguments x and y and immediately returns the result of x + y.
Lambda functions are used where a single line function is sufficient to do the job.
sort as below
sort_by_2nd_value = [(4, 20), (2, 1), (5, 10), (1, 3)]sort_by_2nd_value.sort(key=lambda x: x)print(sort_by_2nd_value)
Can be used in
print(sort_by_2nd_value)from functools import reduceprint(reduce(lambda x, y: x + y, [1, 1, 1, 1, 1]))