"Python: Sort List with Lambda Function - Easy & Fast"

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:

How Python Lambda Sorted with Advanced Examples
How Python Lambda Sorted with Advanced Examples

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:

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

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:

How can I sort a list of dictionaries using a lambda in Python?
How can I sort a list of dictionaries using a lambda in Python?

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.

Python List Methods Everyone Must Know
Python List Methods Everyone Must Know
Python list methods every developer should know.
Python list methods every developer should know.
Python Lists in Python Programming
Python Lists in Python Programming
Python Lists infographic for beginners
Python Lists infographic for beginners
What is Lambda in Python: Lambda() Function with filter, map and reduce
What is Lambda in Python: Lambda() Function with filter, map and reduce
Python List Sort() Method Explained with Examples
Python List Sort() Method Explained with Examples
10 Essential Python List Functions Every Beginner Should Know
10 Essential Python List Functions Every Beginner Should Know
Sort in Python Using Lambda
Sort in Python Using Lambda
Python List Sorting!!
Python List Sorting!!
an important python functions chart with the words and numbers on it, including instructions to use them
an important python functions chart with the words and numbers on it, including instructions to use them
Python lists cheat-sheet infographic
Python lists cheat-sheet infographic
Python list methods
Python list methods
Python Functions Cheat Sheet for Beginners
Python Functions Cheat Sheet for Beginners
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python list methods cheat sheet
Python list methods cheat sheet
Python list sorting cheat sheet
Python list sorting cheat sheet
an info sheet with the words important method in python, set list and keywords
an info sheet with the words important method in python, set list and keywords
a black background with text that reads python lists method, and an image of a computer screen
a black background with text that reads python lists method, and an image of a computer screen
PYTHON BASICS
PYTHON BASICS
Python List Methods With Input , Output
Python List Methods With Input , Output
Python Syntax Data Collections
Python Syntax Data Collections
Python list challenges cheat-sheet
Python list challenges cheat-sheet
Python list operations cheat sheet
Python list operations cheat sheet