Lambda Expressions in Python. How to write anonymous functions in... | by Luay Matalka | Towards Data Science

Imagine we are coding and need to write a simple function. However, we are only going to be using this function once and thus it seems unnecessary to create an entire function with the def keyword for that one task. Well, that’s where lambda expressions come in.
Lambda expressions are used to create anonymous functions, or functions without a name. They are useful when we need to create a function that will only need to be used once (a throw-away function) and can be written in one line. Lambda functions can have any number of parameters but can only have one expression. They generally have this format which yields a function object:
lambda parameters: expression
Let’s say we want to write a function that takes in a number as an input and returns that number squared. We can do so by using the def keyword:
We used the def keyword to define this function. We named this function square. This function has one parameter, num, and it returns that number squared using the ** operator.
Let’s now write this function as a lambda expression:
And that’s it! We first start with the lambda keyword, then the parameter num, a colon, and what you want that function to return, which is num**2.
Note that this function is anonymous, or does not have a name. So we cannot invoke the function at a later point. In addition, we did not write return. Everything after the colon is part of the expression that will be returned.
If we want to assign a lambda function to a variable so that it can be invoked later, we can do so by using an assignment operator:
We can then invoke or call this function the same way we would with a function that was defined with the def keyword. For example:
square(3) # will return 9 as the output
towardsdatascience.com
Let’s make a lambda function that has two parameters instead of just one. First, we will use the def keyword to create a function that returns the sum of two numbers, and then we will write it as a lambda expression:
As we can see, if we want our function to have multiple parameters in a lambda expression, we would just separate those parameters by a comma.
Just like with expressions created using the def keyword, a lambda expression does not need to have any parameters. For example, if we want a lambda expression that takes in no arguments and always returns True, we can write it as follows:
We can also include if else statements in lambda expressions. We would just need to make sure that it is all on one line. For example, let’s create a function that takes in two numbers and returns the greater of those numbers:
Our lambda expression takes in two numbers, num1 and num2, and returns num1 if num1 is greater than num2, else, it returns num2. Obviously this function doesn’t take into account if the numbers are equal, as it will just return num2 in that case, however, we are just illustrating how we can use conditional statements within a lambda expression.
towardsdatascience.com
Technically we cannot use an elif statement in a lambda expression. However, we can nest if else statements within an else statement to achieve the same result as an elif statement. For example, if we also want to check if num1 is greater than num2, if num2 is greater than num1, or else (meaning if they are equal), we can use the following lambda expression:
So if our lambda expression finds that num1 > num2, it will return num1. If this condition is false, it’ll move on to the else statement. Within this else statement (within the parenthesis), it’ll first check if num1 < num2 is true. If that condition is true, it will return num2. If that condition is false, it will return whatever is after the else statement which in this case is the string ‘They are equal’.
Lambda expressions are extremely useful to use in functions that take in another function as an argument. For example, in the map, filter, and reduce functions, we can pass in a lambda expression as the function.
Detailed overview of the map, filter, and reduce functions:
towardsdatascience.com
towardsdatascience.com
In this tutorial, we learned what lambda expressions are, how to write them with zero parameters, one parameter, and multiple parameters. We also learned how to use if else statements within a lambda expression.