Lambdas in Python

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
lambda x : x * 3
lambda x : x * 3
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mult3 = lambda x : x * 3
mult3(15.0) # returns 45
mult3 = lambda x : x * 3 mult3(15.0) # returns 45
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(lambda x : x * 3)(15.0) # returns 45.0
(lambda x : x * 3)(15.0) # returns 45.0
(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.

Syntax

This is the basic syntax of a lambda expression:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
lambda arguments : expression
lambda arguments : expression
lambda arguments : expression

For example, you can create a lambda that squares a number:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pow2 = lambda x: x * x
pow2(5.0) # returns 25.0
pow2 = lambda x: x * x pow2(5.0) # returns 25.0
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sum = lambda a, b : a + b
sum(4.0, 5.0) # returns 9.0
sum = lambda a, b : a + b sum(4.0, 5.0) # returns 9.0
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 def.

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
(lambda x : x * x)(15.0) # returns 225.0
(lambda x : x * x)(15.0) # returns 225.0
(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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
numbers = [1, 2, 3, 4, 5, 6]
filtered_numbers = list(filter(lambda x : x % 2 == 0, numbers))
print(filtered_numbers)
numbers = [1, 2, 3, 4, 5, 6] filtered_numbers = list(filter(lambda x : x % 2 == 0, numbers)) print(filtered_numbers)
numbers = [1, 2, 3, 4, 5, 6]
filtered_numbers = list(filter(lambda x : x % 2 == 0, numbers))
print(filtered_numbers)

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[2, 4, 6]
[2, 4, 6]
[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?

Conclusion

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
lambda arguments : expression
lambda arguments : expression
lambda arguments : expression

As a practical example, you can use a lambda expression to filter even numbers in a list of numbers:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
list(filter(lambda x : x % 2 == 0 , numbers))
list(filter(lambda x : x % 2 == 0 , numbers))
list(filter(lambda x : x % 2 == 0 , numbers))

Thanks for reading. I hope you found this useful!

Further Reading