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

In this tutorial, I will explain how to sort a list of tuples by the second element in Python. Recently in a Python webinar, someone asked me how to sort a list of tuples by the second element. In my research and experiment, I found several effective methods to do this task. Let us learn more about this topic today.

Sort a List of Tuples by the Second Element in Python

Let’s say you’re working on a project that involves analyzing sales data from various states in the USA. You have a list of tuples, where each tuple contains the state name and the corresponding sales figure. Your task is to sort this list of tuples based on the sales figures, which are the second elements in each tuple.

For example, consider the following list of tuples:

sales_data = [("California", 25000), ("Texas", 30000), ("New York", 28000), ("Florida", 22000)]

Our goal is to sort this list in ascending or descending order based on the sales figures.

Read How to Fix the IndexError tuple index out of range Error in Python?

1. Use sort() Method

Python’s built-in sort() method allows us to sort a list in place. We can use this method to sort a list of tuples by the second element. Here’s how we can do it:

sales_data = [("California", 25000), ("Texas", 30000), ("New York", 28000), ("Florida", 22000)]

# Sort the list of tuples by the second element
sales_data.sort(key=lambda x: x[1])

print(sales_data)

Output:

[("Florida", 22000), ("California", 25000), ("New York", 28000), ("Texas", 30000)]

You can refer to the below screenshot to see the output.

Sort a List of Tuples by the Second Element in Python

In this example, we use the sort() method with a key parameter. The key parameter takes a function that specifies the sorting criteria. We pass a lambda function lambda x: x[1] which tells the sort() method to sort based on the second element of each tuple.

Sort in Descending Order

To sort the list of tuples in descending order, we can modify the sort() method slightly:

sales_data = [("California", 25000), ("Texas", 30000), ("New York", 28000), ("Florida", 22000)]

# Sort the list of tuples by the second element in descending order
sales_data.sort(key=lambda x: x[1], reverse=True)

print(sales_data)

Output:

[("Texas", 30000), ("New York", 28000), ("California", 25000), ("Florida", 22000)]

By adding the reverse=True parameter, we tell the sort() method to sort the list in descending order.

Check out How to Create a Tuple from the List in Python?

2. Use sorted() Function

Another way to sort a list of tuples by the second element is by using the sorted() function. Unlike the sort() method, sorted() returns a new sorted list without modifying the original list. Here’s an example:

sales_data = [("California", 25000), ("Texas", 30000), ("New York", 28000), ("Florida", 22000)]

# Sort the list of tuples by the second element using sorted()
sorted_sales_data = sorted(sales_data, key=lambda x: x[1])

print(sorted_sales_data)

Output:

[("Florida", 22000), ("California", 25000), ("New York", 28000), ("Texas", 30000)]

You can refer to the below screenshot to see the output.

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

The sorted() function takes the list of tuples as the first argument and the key parameter specifying the sorting criteria. It returns a new sorted list, leaving the original list unmodified.

Sort in Descending Order with sorted()

To sort the list of tuples in descending order using sorted(), we can pass the reverse=True parameter:

sales_data = [("California", 25000), ("Texas", 30000), ("New York", 28000), ("Florida", 22000)]

# Sort the list of tuples by the second element in descending order using sorted()
sorted_sales_data = sorted(sales_data, key=lambda x: x[1], reverse=True)

print(sorted_sales_data)

Output:

[("Texas", 30000), ("New York", 28000), ("California", 25000), ("Florida", 22000)]

Read How to Find the Length of a Tuple in Python?

3. Use Bubble Sort Algorithm

If you prefer to implement your own sorting algorithm, you can use the Bubble Sort algorithm to sort the list of tuples by the second element. Here’s an example:

def bubble_sort(tuples):
    n = len(tuples)
    for i in range(n - 1):
        for j in range(n - i - 1):
            if tuples[j][1] > tuples[j + 1][1]:
                tuples[j], tuples[j + 1] = tuples[j + 1], tuples[j]
    return tuples

sales_data = [("California", 25000), ("Texas", 30000), ("New York", 28000), ("Florida", 22000)]

# Sort the list of tuples by the second element using Bubble Sort
sorted_sales_data = bubble_sort(sales_data)

print(sorted_sales_data)

Output:

[("Florida", 22000), ("California", 25000), ("New York", 28000), ("Texas", 30000)]

You can refer to the below screenshot to see the output.

Sort a List of Tuples by the Second Element in Python bubble sort

The Bubble Sort algorithm compares adjacent elements and swaps them if they are in the wrong order. It repeats this process until the list is sorted.

Read Python Set vs Tuple

Conclusion

In this tutorial, I explained how to sort a list of tuples by the second element in Python. I discussed the sort() method, sorted() function and sorted in descending order by using them, and using the bubble sort algorithm.

You can read:

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.