Python Lambda Expressions (Anonymous Functions) Tutorial
In this tutorial we learn how to create and use lambda expressions in Python, also known as anonymous functions.
We cover how to declare and pass values to lambdas, as well as using lambdas inside regular functions.
What is a lambda expression
A Lambda expression is an anonymous function. That means, it’s a function that has no name, no return statement and only one expression.
With Lambda expressions we can write less code, and the code will be more readable.
How to declare a lambda expression
To create a lambda expression we use the keyword lambda followed by any number of arguments, and then a colon. Finally, we write the expression.
lambda arguments : expression
The statement above reads: arguments goes to expression
The easiest way to understand a lambda expression is if we convert a normal function into a lambda.
def square(num):
return num * num
The example above takes a number and multiplies it by itself, then returns the result.
To convert this into a lambda is easy.
lambda num : num * num
Here is another way to look at it.
argument num : return num * num
Let’s do another example just to make it clear.
# normal function
def add(num_1, num_2):
return num_1 + num_2
# lambda version
lambda num_1, num_2 : num_1 + num_2
Lambdas can take any number of arguments (before the colon), but only one expression (after the colon).
How to pass values to lambda arguments
At the moment our lambdas are set up, but we can’t use them until we can pass values to the arguments.
To pass a value to an argument we assign the lambda to a data container such as a variable. Then we pass the arguments between parentheses when calling the variable.
# declaration
variable_name = lambda argument : expression
# call
variable_name(argument)
The variable name essentially becomes the lambda’s name like a function would have a name.
# square a num
square = lambda num : num * num
print("Square:", square(5))
# add two numbers
add = lambda num_1, num_2 : num_1 + num_2
print("Add:", add(5, 2))
In the example above we can see that a lambda call looks the same as a function call.
Lambda expression inside a regular function
We can use a lambda inside a normal function. To do this, we declare the lambda inside the normal function’s code block.
def multi(n):
return lambda a : a * n
We declare the lambda as a return statement. The argument passed into the function will be placed into the lambda and the lambda is then returned.
Essentially, if we call the function with the argument 5, the function will return the following:
multi(5)
# returns:
lambda a : a * 5
We now have a lambda returned, but we can’t pass arguments to it so we need to give it a name.
Because the lambda is returned from the function, we can assign the function call to a variable. The variable will then become the lambda name and allow us to pass arguments to it.
def multi(n):
return lambda a : a * n
multiplier = multi(5)
# multiplier variable would now
# technically look like this:
multiplier = lambda a : a * 5
In the example above, we only demonstrate what the multiplier variable would look like after the function’s result is assigned to it. Normally we wouldn’t write the lambda function again into the multiplier variable.
def multi(n):
return lambda a : a * n
multiplier = multi(5)
print(multiplier(10))
The example above seems silly and more typing than needed. But, lambdas inside functions can be very powerful in many situations.
When to use lambda expressions
Lambdas make working with certain interfaces simpler and keep code readable and concise.
Lambdas are usually used to:
- pass arguments to higher-order functions.
- construct the result of a higher-order function that needs to return a function, as we have done above.
A general rule of thumb is to use lambdas in Python for simple functionality and returning lambdas from other functions. If you don’t want to use them or if you find yourself doing something fairly complex, define a normal function.
Summary: Points to remember
- A lambda expression is a function that doesn’t have a name or return statement, and only one expression.
- A lambda is written as argument(s) : return statement .
- When passing values to a lambda expression, we use a function call syntax.
- Lambda expressions can be used inside regular functions.