How to Sort by the Second Element in a Tuple in Python?

As a data analyst, I often encounter datasets where information is stored in tuples, and sorting these tuples based on specific elements is crucial for data manipulation and analysis. For example, you might have a list of employees with their names and salaries, and you want to sort them by salary to identify the highest earners. Let me show you how to do this in Python. In this tutorial, I will explain how to sort a list of tuples by the second element in Python.

Tuples in Python

A tuple is a collection data type in Python that is ordered and immutable. This means that once a tuple is created, its elements cannot be changed. Tuples are defined by placing the elements inside parentheses () and separating them with commas. Here’s an example of a list of tuples containing names and ages of employees:

employees = [("John Doe", 29), ("Jane Smith", 34), ("Emily Davis", 25), ("Michael Johnson", 45)]

In this list, each tuple contains an employee’s name and their age.

Sort A Tuple By Second Element in Python

Python provides different methods to sort tuples. In the previous tutorial on How to Sort a Tuple in Python? I covered a few methods. Here, I will focus on sorting a tuple by second element in Python.

I will show you how to use the built-in sorted() function or the sort() methods to sort a Python tuple by the second element with some real examples.

Using the sorted() Function

The sorted() function in Python returns a new list that is sorted, leaving the original list unchanged. To sort by the second element of each tuple, you can use a lambda function as the key. Here’s how you can do it:

employees = [("John Doe", 29), ("Jane Smith", 34), ("Emily Davis", 25), ("Michael Johnson", 45)]

# Sort by the second element (age)
sorted_employees = sorted(employees, key=lambda x: x[1])

print(sorted_employees)

Output:

[('Emily Davis', 25), ('John Doe', 29), ('Jane Smith', 34), ('Michael Johnson', 45)]

In this example, the lambda x: x[1] function tells Python to sort the tuples based on the second element, which is the age of the employees.

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

Sort A Tuple By Second Element in Python

Check out How to Create a Python Tuple with One Element?

Using the sort() Method

If you want to sort the list in place (modifying the original list), you can use the sort() method. This method does not return a new list but sorts the existing list. Here’s how to use it:

employees = [("John Doe", 29), ("Jane Smith", 34), ("Emily Davis", 25), ("Michael Johnson", 45)]

# Sort in place by the second element (age)
employees.sort(key=lambda x: x[1])

print(employees)

Output:

[('Emily Davis', 25), ('John Doe', 29), ('Jane Smith', 34), ('Michael Johnson', 45)]

Here is the exact output in the screenshot below:

How to Sort by the Second Element in a Tuple in Python

Sort in Descending Order

If you need to sort in descending order, both sorted() and sort() allow you to specify the reverse parameter. Here’s how to sort the list of employees by age in descending order:

employees = [("John Doe", 29), ("Jane Smith", 34), ("Emily Davis", 25), ("Michael Johnson", 45)]

# Sort by age in descending order
sorted_employees_desc = sorted(employees, key=lambda x: x[1], reverse=True)

print(sorted_employees_desc)

Output:

[('Michael Johnson', 45), ('Jane Smith', 34), ('John Doe', 29), ('Emily Davis', 25)]

Check out How to Get the First Element of a Tuple in Python?

Sort by Multiple Criteria

In some cases, you may want to sort by multiple criteria. For example, if you have a list of employees with their names, ages, and salaries, you might want to sort first by age and then by salary. You can achieve this by returning a tuple from the key function. Here’s an example:

employees = [("John Doe", 29, 50000), ("Jane Smith", 34, 60000), ("Emily Davis", 25, 45000), ("Michael Johnson", 34, 70000)]

# Sort by age, then by salary
sorted_employees_multi = sorted(employees, key=lambda x: (x[1], x[2]))

print(sorted_employees_multi)

Output:

[('Emily Davis', 25, 45000), ('John Doe', 29, 50000), ('Jane Smith', 34, 60000), ('Michael Johnson', 34, 70000)]

In this example, employees are sorted primarily by age and secondarily by salary.

Conclusion

In this tutorial, I explained how to sort tuples by second element in Python using different methods such as the sorted() function or the sort() method. I hope all the above examples will help you.

If you have any questions or need further examples, feel free to reach out.

You may also like:

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.