Lambda Expression
You may find yourself in a situation where you need to quickly use a function to perform a simple operation, like filtering a list of numbers for its even numbers. You could write lengthy bits of code or create a function to perform the action might seem too much for such a simple action, especially if you only need to use that function once in your code. In these cases, where a lightweight and simple function is ideal, Python offers a great solution: lambda expressions.
A lambda expression, also known as anonymous functions, are single-use functions that don't have a name, meaning it cannot be called like a regular function (this is why they’re called anonymous functions).
Lambda expressions use the following syntax-
lambda argument(s): expression
- lambda is a Python keyword that defines a lambda expression.
- argument(s) are the arguments the lambda expression takes. You can input any number of arguments, separated by commas.
- The colon : separates the function argument(s) from the function expression.
- expression is the expression that the lambda expression executes upon the given arguments.
Let’s explore lambda expressions by using function add():
def add(x,y):
return x+y
The lambda expression equivalent looks like this-
lambda x,y:x+y
Comparing the two shows you that-
- lambda and def are simply two different ways of defining the function.
- Since lambda expressions are anonymous functions, it doesn't have a function name.
- the arguments behave the exact same way.
- The expressions behave the exact same way.
The main difference is that lambda expressions take up one line while add() takes up two.
This may be trivial now, but it can be quite consequential for writing readable code. There are many built-in functions that can accept functions as arguments and lambda expressions can be particularly useful in these scenarios. For instance, functions like map(), filter(), and sorted() often require a simple function to transform, filter, or order elements. Using a lambda expression with these functions can make your code more concise and readable by integrating the functionality directly where it's needed without the overhead of defining a separate named function.
Let's explore this with the two following scenarios-
Scenario 1- filtering for even numbers
One common use case of lambda involves filtering or transforming objects in a list. Suppose you need to filter a list of numbers for its even numbers. We could do that with the following:
def is_even(number):
"""Check if a number is even."""
return number % 2 == 0
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(is_even, numbers))
print(even_numbers)
Or we could use lambda expressions:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
Scenario 2- sorting with a key
Another common use case is sorting a container based on a condition or element within it. Suppose you have a list of tuples representing people and their ages, and you want to sort this list by age. We could do that with the following:
def get_age(person):
"""Return the age of the person."""
return person[1]
people = [("Alice", 32), ("Bob", 15), ("Catherine", 29)]
sorted_people = sorted(people, key=get_age)
print(sorted_people)
Or could we use lambda expressions:
people = [("Alice", 32), ("Bob", 15), ("Catherine", 29)]
sorted_people = sorted(people, key=lambda person: person[1])
print(sorted_people)
Conclusion
These examples show what lambda expressions excel at- they're incredibly useful for quick data transformations, filtering operations, or applying a simple function over a sequence.
However, it's important to remember that while lambda expressions are powerful and convenient for simple operations, they are not suited for complex functions that would benefit from the clarity provided by a named function definition. The power of lambda expressions lies in its simplicity.
In summary, lambda expressions are a valuable tool in the Python programmer's toolkit, enabling efficient, readable, and elegant coding for a wide range of simple, single-use function scenarios.