What is lambda in Python?


Lambda in Python is an anonymous function which takes any number of arguments but contains only one expression which is to be evaluated and returned.

It is more efficient than using a function having single expression as we need to define the function, pass the parameter, perform the action and finally return the output but in lambda function, we can carry out the expression which is returned and don’t have to assign it to a variable at all.

Lambda function can be used along with built-in functions like filter(), map(), reduce() etc.

Lambda Example


#function to print twice the value of a number passed to function
def double(x):
	return x*2

#the above function can be made as lambda function which 
#takes x as argument and returns twice the value of number passed
double = lambda x : x * 2

print(double(10)) #output 20