Lambda Function In Python

Lambda functions in python are a unique function similar to the regular functions that we create using the def keyword but without any name. This type of function (Lambda Function) is called an anonymous function. However, this Lambda function can only take one line with multiple arguments like the regular one.

Using the Lambda function inside your code is always optional, and you can replace it with the regular ones, but it becomes handy when you want to organize your code and looks more professional. Also, if you will use the function only once in your code, it would be better to use this Lambda function over the regular one.

1. Use The Lambda Function

Creating a Lambda function is so easy since it takes only one line of code but with many arguments, if you want to add more. Let’s see an example:

# Simple Lambda Function
lambda x1, x2: expression

The above code is just the syntax of writing the Lambda function, and you need to start first by typing the “lambda” keyword. It is followed by arguments which are “x1” & “x2” in the above example, and they don’t have to be x1 or x2 just an example to understand. 

Lambda function accepts as many arguments as you need, but in the end, you need only one expression, which is considered any python expression that will be applied or operates on the arguments you’ve provided. Let’s see an example of doing this:

# Creating The Lambda Function
plus = lambda x: x + x

# Calling The Lambda Function
resulte = plus(10)

# Printing The Result
print(resulte)

You maybe get confused about the code, but I will explain. The first line is for creating the lambda function by using the keyword “lambda” followed by one argument, which is x, and the expression that I want to be applied is the adding operations. The Lambda function doesn’t have the return statement like the regular function, but you can call it with its variable and the result.

In the second line, we call the function by its variable name, and we pass the value of the argument x, which is 10 in the above example. It will later return the value 20 when you print it out since the expression we set is x + x, equal to 10 + 10, which gives the 20 value.

2. Multiple Arguments In The Lambda Function

In the previous example, we’ve seen a lambda function with only one argument, but you can have as many arguments as you need in your program, as I’ve mentioned that before. Let’s try an example:

# Creating The Lambda Function
power = lambda x, y: x ** y

# Calling The Lambda Function
resulte = power(10, 2)

# Printing The Result
print(resulte)

You can see that we’ve specified two arguments in the above example, which are x & y. Also, we used them both in the expression, x power y (The two asterisk ** in python is for power calculation), and we store all of this in a variable called power. 

Later, call the power variable and use and specify your x & y values 10 and 2 in the second line of this example. When you run the code, it will print out the 100 value.

3. Adding Conditions To a Lambda Function

We’ve talked that the Lambda function is the same as the regular function, and it can also take expressions like the other function and return True or False. Let’s see the syntax of this:

lambda argument: x if the_boolean_expression else y

Everything looks the same as we saw how the Lambda function works. If you know how to use the if-else statement, you will understand by yourself how I work here in the Lambda function.

If the condition were True, it would execute the x variable, and if the condition is False, then the y variable will be executed. Let’s see an example:

# Creating The Lambda Function
times = lambda x, y: x * 3 if (x > 2) else y

# Calling The Lambda Function
resulte = times(5, 6)

# Printing The Result
print(resulte)

The code will print the result 15, which means it executed the first expression since 5 * 3 is 15 and is bigger than the value y, which is 6. We’ve species this value when we called it in the second line of this example.

4. Lambda Default Argument

I will give you an example at first to understand this. Let’s suppose you calculate how many kilometers in a specific mile value. The equation is that 1 mile in 1.6 kilometers, so the 1.6 value is always the same, but how we can make default parameters in this situation. Let’s see an example:

# Creating The Lambda Function
kilometers = lambda x, y = 1.6: (x * 1.6)

# Calling The Lambda Function
resulte = kilometers(10)

# Printing The Result
print(resulte)

You can see from this example that y is the default argument which is 1.6, and when you call the function, you will need to specify just the kilometers value. It will be times 1.6, and the example shows that 10 miles are 16 kilometers.

Conclusion

Thanks for reading! The Lambda function has more options and uses than what we’ve discussed in this short tutorial. Hopefully, this short tutorial will help you understand how to use this function, at least with its basic usage. You can see the official documentation for more information about this function.

Scroll to Top