Sorting a list in Python is a common task, and while the built-in `sort()` function is powerful, sometimes you need more control over the sorting process. This is where the `lambda` function comes in handy. In this article, we'll explore how to use `lambda` to sort a list in Python in a clear and efficient manner.
Understanding Lambda Functions
Before diving into sorting lists, let's briefly recap what lambda functions are. Lambda functions, also known as anonymous functions, are small, one-line functions that can be used wherever function objects are required. They are defined using the `lambda` keyword, followed by one or more arguments separated by commas, and a colon (`:`) followed by an expression.
Syntax of Lambda Functions
The general syntax of a lambda function is as follows:

lambda arguments: expression
Where:
- arguments: are the inputs to the function. Multiple arguments are separated by commas.
- expression: is the code that is executed and the result of the function.
Sorting a List with Lambda
Now that we've refreshed our understanding of lambda functions, let's see how we can use them to sort a list. The `sort()` function in Python allows us to pass a key parameter, which is a function that takes one argument and returns a key to use for sorting purposes. This is where lambda functions shine.
Sorting a List of Tuples
Let's consider a list of tuples, where each tuple contains a name and an age. We want to sort this list by age:

people = [('Alice', 30), ('Bob', 25), ('Charlie', 35)]
We can use the `sort()` function with a lambda function as the key parameter to achieve this:
people.sort(key=lambda x: x[1])
Here, the lambda function takes a tuple `x` as input and returns `x[1]`, which is the age. The `sort()` function uses this age to sort the list.
Sorting a List of Objects
What if our list contains objects with attributes? We can still use lambda functions to sort based on these attributes. Let's consider a list of `Person` objects:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age
people = [Person('Alice', 30), Person('Bob', 25), Person('Charlie', 35)]
We can sort this list by age using the following code:
people.sort(key=lambda x: x.age)
Here, the lambda function takes a `Person` object `x` as input and returns `x.age`.
Sorting in Descending Order
By default, the `sort()` function sorts in ascending order. If we want to sort in descending order, we can use the `reverse` parameter of the `sort()` function:
people.sort(key=lambda x: x.age, reverse=True)
Alternatively, we can modify the lambda function to return the negation of the age, which will sort the list in descending order:
people.sort(key=lambda x: -x.age)
Sorting a List Using the `sorted()` Function
While the `sort()` function sorts the list in-place, the `sorted()` function returns a new sorted list and leaves the original list unaffected. Here's how we can use `sorted()` with a lambda function:
sorted_people = sorted(people, key=lambda x: x.age)
This will give us a new list `sorted_people` that is sorted by age, while the original `people` list remains unchanged.
Conclusion
In this article, we've explored how to use lambda functions to sort lists in Python. We've seen how to sort lists of tuples and objects, and how to sort in ascending and descending order. Whether you're using the `sort()` function or the `sorted()` function, lambda functions provide a concise and powerful way to sort lists in Python.






















