Lambda Functions in Python: A Comprehensive Guide to Understanding and Using Anonymous Functions (32/100 Days of Python)

Martin Mirakyan
3 min readFeb 2, 2023

--

Day 32 of the “100 Days of Python” blog post series covering lambda functions

One of the most useful features of Python is the ability to create anonymous functions using the lambda keyword. These functions, known as lambda functions, are small, one-time-use functions that can be used as arguments for other functions. In this article, we will provide a comprehensive guide on how to understand and use lambda functions in Python.

Syntax of Lambda Functions

The syntax for a lambda function in Python is quite straightforward. It is defined using the lambda keyword, followed by a comma-separated list of arguments, and a single expression that the function returns:

lambda arguments: expression

For example, to create a lambda function that takes two arguments and returns their sum, we would write the following code:

add = lambda x, y: x + y
result = add(5, 3)
print(result) # 8

Using Lambda Functions with Higher-Order Functions

One of the common uses of lambda functions in Python is as arguments to higher-order functions such as map, filter, and reduce. These functions allow us to perform operations on lists and other iterables, making it easier to manipulate data. Let's look at each of these functions in detail.

Using Lambda Functions with map

The map function is a built-in Python function that takes a function and an iterable as arguments and returns a new iterable with the function applied to each element. For example, to square all the elements of a list of numbers using a lambda function and map, we would write the following code:

numbers = [1, 2, 3, 4, 5]
square = list(map(lambda x: x**2, numbers))
print(square) # [1, 4, 9, 16, 25]

In this code, the list numbers is first mapped from original values to their squares with the map(lambda x: x**2, numbers). After which the result is converted into a list.

Using Lambda Functions with filter

The filter function is another built-in Python function that takes a function and an iterable as arguments and returns a new iterable with only the elements for which the function returns True. For example, to get a list of even numbers from a list of numbers using a lambda function and filter, we would write the following code:

numbers = [1, 2, 3, 4, 5]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens) # [2, 4]

Using Lambda Functions with reduce

The reduce function is a built-in function from the functools module that takes a function and an iterable as arguments and returns a single value that is the result of applying the function cumulatively to the elements of the iterable. For example, to find the product of all the elements of a list of numbers using a lambda function and reduce, we would write the following code:

from functools import reduce

numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product) # 120

Limitations of Lambda Functions

While lambda functions are incredibly useful, they do have a few limitations that are important to keep in mind. One of the main limitations of lambda functions is that they can only contain a single expression. This means that they cannot contain statements or multiple lines of code. Additionally, the expression must be a valid expression in Python and must return a value. Another limitation is that lambda functions cannot contain any annotations or type hints.

Despite these limitations, lambda functions are still a powerful tool in the Python programmer’s toolkit. They are especially useful for short, one-time-use functions that are used as arguments to higher-order functions or for quickly defining simple functions in the context of another function.

What’s next?

--

--