Lambdas in Python are anonymous functions that take any number of arguments but only contain a single expression.
As an example, here is a lambda that multiplies a number by three:
lambda x : x * 3
This lambda isn’t particularly useful as-is because there’s no way to call it. However, you could assign it to a variable and then call it like a regular function:
mult3 = lambda x : x * 3 mult3(15.0) # returns 45
But you don’t actually need or even should assign a lambda to a variable.
Instead, you should call it right away like this:
(lambda x : x * 3)(15.0) # returns 45.0
Lambda functions are useful when the functionality is needed for only a short period of time. This is when you don’t want to waste resources by creating a separate method to do the job.
If you find yourself assigning a lambda expression to a variable, use a regular function instead.
How To Use Lambdas in Python
You already saw some basic examples of lambdas, but let’s take a closer look at how they work.
This is the basic syntax of a lambda expression:
lambda arguments : expression
For example, you can create a lambda that squares a number:
pow2 = lambda x: x * x pow2(5.0) # returns 25.0
As another example, here is a lambda expression that takes more than one argument:
sum = lambda a, b : a + b sum(4.0, 5.0) # returns 9.0
Tip: Lambdas are not useful this way. If you want to assign a lambda to a variable, just create a normal function with
Lambdas in Python Are Anonymous Functions
Lambdas are also known as anonymous functions. To demonstrate why, let’s jump back to the example of squaring a number. This time, let’s not assign the lambda to a variable but call it directly instead:
(lambda x : x * x)(15.0) # returns 225.0
This example highlights the fact that a lambda function is an anonymous function (i.e. it has no name, but you can still call it).
When To Use Lambdas
As I mentioned, the examples you saw are somewhat useless. So what is the whole point of using lambdas?
Lambda functions are useful when you need the functionality for a short period of time (for example passing a function as an argument to another function).
Let’s see a couple of examples where lambdas are really useful.
Example—Filter a list of numbers
A practical example of passing a function to another function is when you are filtering a list. Python’s built-in
filtermethod takes two parameters:
- A filtering function (this is a lambda function)
- A list to be filtered
As an example, let’s filter even numbers from a list:
numbers = [1, 2, 3, 4, 5, 6] filtered_numbers = list(filter(lambda x : x % 2 == 0, numbers)) print(filtered_numbers)
[2, 4, 6]
In the code above, the filtering expression
lambda x : x % 2 == 0 is evaluated on each number in the
numbers list so that the odd numbers are left out.
This is a good example of when lambdas are really useful. Since you only need the filtering function for filtering the list, why would you create a separate
is_even method that you would never use again?
Lambda functions are anonymous functions that can be used in place of regular functions. They are useful when the functionality is needed for only a short period of time.
Lambda expressions follow this simple syntax:
lambda arguments : expression
As a practical example, you can use a lambda expression to filter even numbers in a list of numbers:
list(filter(lambda x : x % 2 == 0 , numbers))
Thanks for reading. I hope you found this useful!
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.01.10How to Use AI Art Generators—Input Design (Prompt Engineering)
- Favorites2022.11.30ChatGPT—What Is It? How to Use It? (Examples, Tips & More)