Recently, I was working on a project that involved analyzing sales data for a company based in the United States. The data was provided as a list of tuples, where each tuple contained information. To gain meaningful insights from this data, I needed to sort the list of tuples based on different criteria. In this article, I will share my experience and demonstrate how to sort a list of tuples in Python using various techniques.
Sort a List of Tuples in Python
To sort a list of tuples in Python, you can use the sorted() function with a key parameter that specifies the sorting criteria. For example, to sort a list of tuples [('John', 25), ('Emma', 30), ('Michael', 20)] by the second element (age) in ascending order, you can use sorted(tuples, key=lambda x: x[1]), which will return [('Michael', 20), ('John', 25), ('Emma', 30)].
Read How to Find the Length of a List in Python?
1. Use the sort() Method
One way to sort a list of tuples is by using the built-in sort() method in Python. This method modifies the original list in place and sorts the tuples based on the specified criteria. Let’s take a look at an example:
sales_data = [
('John', 'iPhone', 1200),
('Emma', 'MacBook', 2500),
('Michael', 'iPad', 800),
('Sophia', 'iPhone', 1100),
('William', 'MacBook', 2200)
]
sales_data.sort(key=lambda x: x[2])
print(sales_data)Output:
[('Michael', 'iPad', 800), ('Sophia', 'iPhone', 1100), ('John', 'iPhone', 1200), ('William', 'MacBook', 2200), ('Emma', 'MacBook', 2500)]You can look at the screenshot below to see the output.

In this example, we have a list of tuples called sales_data representing sales information. Each tuple contains the salesperson’s name, the product sold, and the revenue generated. To sort the list based on the revenue (the third element of each tuple), we use the sort() method with a key parameter.
The key parameter takes a function that specifies the sorting criteria. In this case, we use a lambda function lambda x: x[2] which means we want to sort based on the third element of each tuple. The sort() method then modifies the original list, sorting the tuples in ascending order of revenue.
Check out How to Get the Last Element of a List in Python?
2. Use the sorted() Function
Another way to sort a list of tuples is by using the sorted() function in Python. Unlike the sort() method, the sorted() function returns a new sorted list without modifying the original list. Here’s an example:
sales_data = [
('John', 'iPhone', 1200),
('Emma', 'MacBook', 2500),
('Michael', 'iPad', 800),
('Sophia', 'iPhone', 1100),
('William', 'MacBook', 2200)
]
sorted_data = sorted(sales_data, key=lambda x: x[1])
print(sorted_data)Output:
[('Emma', 'MacBook', 2500), ('William', 'MacBook', 2200), ('Michael', 'iPad', 800), ('John', 'iPhone', 1200), ('Sophia', 'iPhone', 1100)]You can look at the screenshot below to see the output.

In this example, we use the sorted() function to sort the sales_data list based on the product name (the second element of each tuple). The key parameter is set to lambda x: x[1] indicate that we want to sort based on the second element. The sorted() function returns a new list sorted_data with the tuples sorted alphabetically by product name.
Read How to Add Tuples to Lists in Python?
Sort in Descending Order
By default, both the sort() method and the sorted() function sorts the tuples in ascending order. However, you can easily sort in descending order by specifying the reverse parameter as True. Here’s an example:
sales_data = [
('John', 'iPhone', 1200),
('Emma', 'MacBook', 2500),
('Michael', 'iPad', 800),
('Sophia', 'iPhone', 1100),
('William', 'MacBook', 2200)
]
sales_data.sort(key=lambda x: x[2], reverse=True)
print(sales_data)Output:
[('Emma', 'MacBook', 2500), ('William', 'MacBook', 2200), ('John', 'iPhone', 1200), ('Sophia', 'iPhone', 1100), ('Michael', 'iPad', 800)]You can look at the screenshot below to see the output.

In this example, we use the sort() method with the reverse parameter set to True. This sorts the sales_data list in descending order based on the revenue.
Read How to Select Items from a List in Python?
Sort by Multiple Criteria
You can also sort a list of tuples based on multiple criteria by returning a tuple from the key function. Here’s an example:
sales_data = [
('John', 'iPhone', 1200),
('Emma', 'MacBook', 2500),
('Michael', 'iPad', 800),
('Sophia', 'iPhone', 1100),
('William', 'MacBook', 2200)
]
sorted_data = sorted(sales_data, key=lambda x: (x[1], x[2]))
print(sorted_data)Output:
[('Michael', 'iPad', 800), ('Sophia', 'iPhone', 1100), ('John', 'iPhone', 1200), ('William', 'MacBook', 2200), ('Emma', 'MacBook', 2500)]In this example, we sort the sales_data list based on two criteria: first by the product name (the second element of each tuple) and then by the revenue (the third element). The key function returns a tuple (x[1], x[2]), specifying the sorting order. The tuples are first sorted alphabetically by product name, and if there are any ties, they are further sorted by revenue.
Check out How to Write a List to a File in Python?
Applications
Sorting lists of tuples has numerous real-world applications across various domains. Here are a few examples:
- Sales Analysis: In the examples throughout this tutorial, we used sales data to demonstrate sorting techniques. Sorting sales data by revenue, product, or salesperson can help identify top-performing products, high-revenue salespeople, and make data-driven business decisions.
- Student Records: Educational institutions often store student records as tuples containing information such as student name, ID, grades, and course enrollment. Sorting these records alphabetically by name or by academic performance can facilitate easy lookup and analysis.
- Inventory Management: Retailers and warehouses maintain inventory records as tuples, including product names, quantities, and prices. Sorting inventory data by product name, quantity, or price can help optimize stock management and identify popular or low-stock items.
- Customer Data: Companies often store customer information as tuples, including name, email, purchase history, and preferences. Sorting customer data alphabetically by name or by purchase history can aid in targeted marketing campaigns and personalized recommendations.
Read How to Iterate Through a List in Python?
Conclusion
In this tutorial, I helped you learn how to sort a list of tuples in Python. I explained a few methods, such as using the sort() method and the sorted() function. I also covered sorting in descending order and sorting by multiple criteria using lambda functions.
You may like to read:
- How to Merge Lists Without Duplicates in Python?
- How to Convert String to List in Python?
- Convert String to List in Python Without Using Split

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.