Sort a List of Tuples by Second Item - Python
Last Updated : 18 Feb, 2025
The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)], the goal is to reorder it by the second item, resulting in [(4, 1), (2, 2), (1, 3)].
Using sorted()
sorted() is the most versatile way to sort data. It takes an iterable and returns a new sorted list without modifying the original list. We can specify the sorting key using the key parameter which allows us to sort the tuples based on the second item.
Python a = [(1, 3), (4, 1), (2, 2)] # Sort the list based on the second element res = sorted(a, key=lambda x: x[1]) print(res)
Output[(4, 1), (2, 2), (1, 3)]
Explanation: sorted(a, key=lambda x: x[1]) extract the second element (x[1]) from each tuple for comparison and list is returned in sorted order by the second item of each tuple.
Using sort()
Unlike sorted(), the sort() method sorts the list in place meaning it directly modifies the original list without returning a new one. This method is more efficient when we do not need to preserve the original list order.
Python a = [(5, 2), (1, 6), (3, 4)] # Sort the list in-place based on the second element a.sort(key=lambda x: x[1]) print(a)
Output[(5, 2), (3, 4), (1, 6)]
Explanation: a.sort(key=lambda x: x[1]) sorts the list a in place based on the second item of each tuple and result is directly stored in the original list a and no new list is created.
Using itemgetter
itemgetter() from the operator module provides a more efficient and readable way to retrieve the sorting key. It is often faster than using a lambda function, especially for larger datasets.
Python from operator import itemgetter a = [(7, 5), (3, 8), (2, 6)] # Sort the list based on the second element res = sorted(a, key=itemgetter(1)) print(res)
Output[(7, 5), (2, 6), (3, 8)]
Explanation: itemgetter(1) retrieves the second item (1) from each tuple. It is more efficient than using a lambda function and sorted() sorts the list based on the second item of each tuple.
Similar Reads
Python - Sort by Frequency of second element in Tuple List Given list of tuples, sort by frequency of second element of tuple. Input : test_list = [(6, 5), (1, 7), (2, 5), (8, 7), (9, 8), (3, 7)] Output : [(1, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)] Explanation : 7 occurs 3 times as 2nd element, hence all tuples with 7, are aligned first. Input : test_l
6 min read
How To Slice A List Of Tuples In Python? In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i
3 min read
Sort Tuple of Lists in Python The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
3 min read
Sort the values of first list using second list in Python Sorting the values of the first list using the second list in Python is commonly needed in applications involving data alignment or dependency sorting. In this article, we will explore different methods to Sort the values of the first list using the second list in Python.Using zip() and sorted() zip
3 min read
Custom Sorting in List of Tuples - Python The task of custom sorting in a list of tuples often involves sorting based on multiple criteria. A common example is sorting by the first element in descending order and the second element in ascending order. For example, given a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)], sorting the tuples by t
3 min read