In Python, a Lambda Function
is a single-lined function that is defined without an explicit name. They are also known as anonymous functions.
While the def
keyword is used to define named functions, the lambda
keyword is used to define anonymous functions i.e lambda functions.
The lambda functions are more lightweight and concise compared to named functions, this makes them ideal for simple operations that do not require complex logic.
To define this type of functions, we use lambda keyword with arguments and the expression as shown below:
lambda arguments : expression
copy
The comma-separated arbitrary arguments are separated from the function expression/body by a colon. We can only use one expression in the functions body.
Note: lambda functions can have any number of arguments but only a single expression.
Consider the following function:
def func(x): return x
copy
The equivalent lambda function to the above function is as follows:
lambda x : x
copy
We do not need to explicitly use the return
statement with lambda functions because it automatically returns the value of the expression after evaluation. Other examples:
a lambda function to add two numbers:
lambda a, b : a + b
copy
The above lambda function takes two arguments a
and b
and returns the sum of those two arguments (a + b)
.
a lambda function to tell whether a number is odd or even:
lambda num : "Odd" if num % 2 else "Even"
copy
The above lambda function will return 'Odd' if the number is odd , else it will return 'Even'.
Calling lambda functions
A lambda functions can be called directly by enclosing it with parentheses, For example:
However, in order to make the lambda functions persist and be usable multiple times, we can assign them to a variable, then use the variable name to call the function. Example:
This is not unique to lambda functions, we can also assign variable names to normal functions, however, the normal function retains it's declaration name because the name is associated with the function object itself.
Uses of lambda functions
Lambda functions are in some cases more cryptic than the 'normal' functions because you have to compress all the function logic into one expression. Additionally, anything that you can achieve with lambda
functions, you can also effectively accomplish it with the named functions. There are some instances, however, when lambda
functions are more suited than the regular functions.
For simple transformation
The lambda
functions can be more suitable if all we want is to transform an object in a simple way.
As arguments to higher-order functions
In Python and other functional languages, it is common to pass functions as arguments to other functions. In such cases, lambda
functions are often used because they can be defined inline. Some builtin such functions includes:
The map()
functions:
The map()
function is used to perform an action on each item in a collection.
The filter()
functions
This function is used to filter values in given sequence by their boolean values, based on a given function. Examples:
The functools.reduce()
function
This function applies a function of two arguments cumulatively on a sequence of elements from left to right, reducing the sequence to a single value.
As key Functions
Lambda expressions can be used as keys in functions and methods that allow customization using functions as keys.
For example in the builtin sorted()
function and the sort()
method of lists, a lambda
expression can be used to define the sorting criteria.
In the above example, we use the lambda
expression to indicate to the sorted()
function that it should sort the list elements(which are tuples) based on their second elements(at index 1
). If no key was given the function would have sorted the list element alphabetically, based on their first element, therefore, the sorted list would have been [('Jane', 25), ' ('John', 30), ('Joe', 20)]
.
Other builtin functions that allows key argument includes max()
and min()
.