Lambda Functions and Anonymous Functions in Python
def keyword is used to define a function in Python, as we have seen in the previous chapter. The
lambda keyword is used to define anonymous functions in Python.
Usually, such a function is meant for one-time use.
lambda [arguments] : expression
The lambda function can have zero or more arguments after the
When this function is called, the expression after
: is executed.
square = lambda x : x * x
Above, the lambda function starts with the
lambda keyword followed by parameter
x * x after
: returns the value of
x * x to the caller.
The whole lambda function
lambda x : x * x is assigned to a variable
square in order to call it like a named function.
The variable name becomes the function name so that We can call it as a regular function, as shown below.
>>> square(5) 25
The above lambda function definition is the same as the following function:
def square(x): return x * x
The expression does not need to always return a value. The following lambda function does not return anything.
>>> greet = lambda name: print('Hello ', name) >>> greet('Steve') Hello Steve
The following lambda function contains multiple parameters:
>>> sum = lambda x, y, z : x + y + z >>> sum(5, 10, 15) 30
The following lambda function can take any number of parameters:
>>> sum = lambda *x: x+x+x+x >>> sum(5, 10, 15, 20) 50
Parameterless Lambda Function
The following is an example of the parameterless lambda function.
>>> greet = lambda : print('Hello World!') >>> greet() Hello World!
We can declare a lambda function and call it as an anonymous function, without assigning it to a variable.
>>> (lambda x: x*x)(5) 25
lambda x: x*x defines an anonymous function and call it once by passing arguments in the parenthesis
(lambda x: x*x)(5).
In Python, functions are the first-class citizens, which means that just as literals, functions can also be passed as arguments.
The lambda functions are useful when we want to give the function as one of the arguments to another function. We can pass the lambda function without assigning it to a variable, as an anonymous function as an argument to another function.
>>> def dosomething(fn): print('Calling function argument:') fn() >>> dosomething(lambda : print('Hello World')) # passing anonymous function Calling function argument: Hello World >>> myfn = lambda : print('Hello World') >>> dosomething(myfn) # passing lambda function
dosomething() function is defined with the
fn parameter which is called as a function inside
dosomething(lambda : print('Hello World')) calls the
dosomething() function with an anonymous lambda function as an argument.
Python has built-in functions that take other functions as arguments. The map(), filter() and reduce() functions are important functional programming tools. All of them take a function as their argument. The argument function can be a normal function or a lambda function.
>>> sqrList = map(lambda x: x*x, [1, 2, 3, 4]) # passing anonymous function >>> next(sqrList) 1 >>> next(sqrList) 4 >>> next(sqrList) 9 >>> next(sqrList) 16 >>> next(sqrList) 25