Understanding Lambdas: Single Expression functions in Python
Understanding the single expression functions in Python
In this article, we’ll learn about the anonymous function, also known as lambda functions. We’ll try to decode as to why there’s a big fuss about lambdas, understand their syntax and when to use them.
So Let’s jump right in.
The lambda keyword in Python provides a shortcut for declaring small anonymous functions. Lambda functions behave just like regular function declared with def keyword. They can be used whenever function objects are required.
For example, this is how we’d define a simple lambda function carrying out an addition:
>>> add = lambda x, y: x + y
>>> add(22, 10)
32
We can declare the same add function with the def keyword, but it would be slightly more verbose:
>>> def add(x, y):
... return x + y
>>> add(22, 10)
32
Now we might be wondering, why bother about lambdas so much, when they’re just a slightly more concise version of declaring functions with def keyword.🤷🏻
Let’s have a look at another example.
>>> (lambda x, y: x + y)(22, 10)
32
In above example, we’ve used lambda to define an add function inline and then called it immediately with arguments 22 and 10.
Conceptually, the lambda expression lambda x, y: x + y
is the same as declaring a function with def, but just written inline. The key difference here is that we didn’t have to bind the function object to a name before we used it. We simply stated the expression we wanted to compute as part of a lambda, and then immediately evaluated it by calling the lambda expression like a regular function.
Another difference between lambdas and regular functions definitions is that, lambda functions are restricted to single expression. This means a lambda function can’t use statements or annotations — not even a return statement.
Additionally, executing a lambda function evaluates its expression and then automatically returns the expression’s result, so there’s always an implicit return statement. Therefore, they are also referred to as single expression functions.
When to use Lambdas
Anytime we’re expected to supply a function object, we can use a lambda expression. And because lambdas can be anonymous, we don’t even need to assign them to a name first.
Most frequent use case for lambdas is writing short and concise key funcs for sorting iterables by an alternate key:
>>> words = [('Sarah', 22), ('Roark', 30), ('Wilson', 6)]# By default sort() will sort the list by its first item in tuples
>>> words.sort()
>>> words
[('Roark', 30), ('Sarah', 22), ('Wilson', 6)]>>> sorted(words, key=lambda x: x[1])
[('Wilson', 6), ('Sarah', 22), ('Roark', 30)]
In above example, we’re sorting a list of tuples by the second value in each tuple. Let’s look another example to see how using a lambda can give us much more flexibility:
>>> sorted(range(-2, 6), key=lambda x: x * x)
[0, -1, 1, -2, 2, 3, 4, 5]
It should be duly noted however that if we find ourself doing anything complex with lambda expressions, defining a standalone function with a proper name should be considered, after-all clean and readable code is way more appreciated.😄
# Not Recommended>>> list(filter(lambda x: x % 2 == 0, range(16)))
[0, 2, 4, 6, 8, 10, 12, 14]
Conclusions:
- Lambdas functions are single-expression functions that are not necessarily bound to a name.
- Lambda functions can’t use regular Python statement and always include an implicit return statement.