What is lambda function in Python
A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.
lambda arguments : expression
The expression is executed and the result is returned.
x = lambda a : a + 5 print(x(3))
The output of the above piece of code will be 8. Here 5 is added to the argument and result is returned. i.e. 3+5=8
x = lambda a, b : a ** b print(x(3, 4))
Lambda function can take any number of arguments but should contain only one expression. In this example, we are passing two arguments and executing the expression given. So the output of this example will be, 3**4 = 81.
li = [2, 5, 7, 54, 23, 77, 21, 26, 78] re = list(filter(lambda x: (x%2 !=0), li)) print(re)
In this example, initially we have a list. We are using lambda function to filter out the odd numbers from the list.