In Post #106, we introduced lambda
functions as a way to create small, anonymous, one-line functions. We mentioned that their primary purpose is to be used as quick, “throwaway” functions that are passed as arguments to higher-order functions.
In this post, we’ll see exactly what that means. We will explore three of Python’s most common higher-order functions—sorted()
, map()
, and filter()
—and see how lambda
functions make them incredibly powerful and concise.
Use Case 1: Custom Sorting with sorted()
We’ve seen the sorted()
function before in Post #52. What we didn’t cover is its optional key
parameter. You can pass a function to this key
parameter, and sorted()
will call that function on each item before making its comparisons. The sort order is then based on the results of the function call, not the original items themselves.
This is where lambda
shines. Let’s say we have a list of tuples, where each tuple is a player’s name and their score. We want to sort the list based on the score (the second item in the tuple).
# A list of (name, score) tuples
leaderboard = [("Alice", 88), ("Bob", 92), ("Charlie", 75)]
# Sort the list by the score (the item at index 1 of each tuple).
# The lambda function tells sorted() to look at the second item.
sorted_board = sorted(leaderboard, key=lambda player: player[1], reverse=True)
print(sorted_board)
The output will be the list of players sorted from highest to lowest score:
[(‘Bob’, 92), (‘Alice’, 88), (‘Charlie’, 75)]
The lambda player: player[1]
is a tiny, anonymous function that takes a tuple (player
) and returns the item at index 1 (the score). sorted()
uses these returned scores (92, 88, 75) to determine the final order of the original tuples.
Use Case 2: Transforming Items with map()
The map(function, iterable)
function is used to apply a given function
to every single item in an iterable
(like a list). It returns a special map
object, which is an iterator containing all the results.
A lambda
is perfect for providing a simple transformation rule. Let’s say we want to square every number in a list.
numbers = [1, 2, 3, 4, 5]
# The lambda x: x**2 is applied to each number in the list.
squared_numbers_iterator = map(lambda x: x ** 2, numbers)
# map() returns an iterator, so we convert it to a list to see the results.
squared_numbers_list = list(squared_numbers_iterator)
print(squared_numbers_list)
The output will be: [1, 4, 9, 16, 25]
.
Use Case 3: Filtering Items with filter()
The filter(function, iterable)
function is used to create an iterator from the items of an iterable
for which the provided function
returns True
. It’s a way of filtering out items based on a condition.
A lambda
is ideal for defining that condition in a single line. Let’s filter a list to get only the even numbers.
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# The lambda x: x % 2 == 0 returns True for even numbers and False for odd ones.
even_numbers_iterator = filter(lambda x: x % 2 == 0, numbers)
# Like map(), filter() returns an iterator, so we convert it to a list.
even_numbers_list = list(even_numbers_iterator)
print(even_numbers_list)
The output will be: [2, 4, 6, 8, 10]
.
What’s Next?
You’ve now seen the true power of lambda
functions. They act as quick, custom-made tools that you can pass to other functions like sorted()
, map()
, and filter()
to define a specific behavior on the fly. This is a very common and powerful pattern in Python.
We’ve seen that lambda
functions are concise, but def
functions are more powerful. So when should you choose one over the other? In Post #108, we will lay out some clear guidelines on when a lambda
is appropriate and when a full def
function is the better choice.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com