How to Sort a List of Tuples by the First Element in Python?

Recently, one of my team members struggled to sort a list of tuples by the first element in Python. I suggested different methods. In this tutorial, I will show you how to sort a list of tuples by the first element in Python with examples.

To sort a list of tuples by the first element in Python, you can use the built-in sorted() function along with a lambda function as the key. For example, given a list of tuples employees = [(104, 'Alice'), (102, 'Bob'), (101, 'Charlie'), (103, 'David')], you can sort it by the first element (e.g., employee ID) using sorted_employees = sorted(employees, key=lambda x: x[0]). This will return a new sorted list [(101, 'Charlie'), (102, 'Bob'), (103, 'David'), (104, 'Alice')] without modifying the original list.

Method 1: Using the sorted() Function

The sorted() function is a built-in Python method that returns a new sorted list from the elements of any iterable. It is highly versatile and can be customized using the key parameter.

Now, let me show you an example to help you understand this.

Example: Sorting Employee Data

Suppose you have a list of tuples where each tuple contains an employee’s ID and name. You want to sort this list by the employee ID.

employees = [(104, 'Alice'), (102, 'Bob'), (101, 'Charlie'), (103, 'David')]

# Sorting by the first element (employee ID)
sorted_employees = sorted(employees, key=lambda x: x[0])

print(sorted_employees)

Output:

[(101, 'Charlie'), (102, 'Bob'), (103, 'David'), (104, 'Alice')]

In this example, we used a lambda function lambda x: x[0] as the key to sort the list by the first element of each tuple.

Here is the output you can see the screenshot below:

python sort list of tuples by first element

Check out How to Get the Index of an Element in a List in Python?

Method 2: Using the sort() Method

The sort() method in Python sorts the list in place and modifies the original list. This method is useful when you do not need to keep the original list.

Here is an example and the complete Python code.

Example: Sorting Product Prices

Suppose you have a list of tuples where each tuple contains a product ID and its price. You want to sort this list by the product ID.

products = [(2002, 19.99), (2001, 29.99), (2004, 9.99), (2003, 14.99)]

# Sorting by the first element (product ID)
products.sort(key=lambda x: x[0])

print(products)

Output:

[(2001, 29.99), (2002, 19.99), (2003, 14.99), (2004, 9.99)]

Here, the sort() method directly sorts the products list by the first element of each tuple.

You can see the output in the screenshot below:

sort a list of tuples by first value python

Method 3: Using itemgetter from operator Module

The itemgetter function from the operator module can also be used to sort a list of tuples in Python. This method is often more readable and can be slightly more efficient.

Example: Sorting Student Scores

Consider a list of tuples where each tuple contains a student’s ID and their score. You want to sort this list by the student ID.

from operator import itemgetter

students = [(3002, 85), (3001, 90), (3004, 75), (3003, 80)]

# Sorting by the first element (student ID)
sorted_students = sorted(students, key=itemgetter(0))

print(sorted_students)

Output:

[(3001, 90), (3002, 85), (3003, 80), (3004, 75)]

In this example, itemgetter(0) is used to specify that the list should be sorted by the first element of each tuple.

Here is the output in the screenshot below:

sort list of tuples by first element python

Check out List Comprehension If Else in Python

Python: Sort List of Tuples by First Element Example

Let’s consider a real-world scenario where you have a list of tuples containing state names and the number of COVID-19 cases. You want to sort this list by the state names.

covid_data = [('California', 500000), ('Texas', 400000), ('Florida', 300000), ('New York', 200000)]

# Sorting by the first element (state name)
sorted_covid_data = sorted(covid_data, key=lambda x: x[0])

print(sorted_covid_data)

Output:

[('California', 500000), ('Florida', 300000), ('New York', 200000), ('Texas', 400000)]

Here, we sorted the list of tuples by state names, which can be useful for displaying data in a more readable format.

I executed the above Python code, and you can see the output in the screenshot below:

python sort list of tuples by first and second element

Conclusion

In this tutorial, I explained to you how to sort a list of tuples by the first element in Python using different methods like the built-in sorted() function, the sort() method, and the itemgetter function from the operator module.

Also, I have explained a real-world example. I hope this helps.

You may also like the following tutorials:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.