Sort a List of Tuples by Second Item - Python
Last Updated :
11 Jul, 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 a tuple by its float element In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting. Examples: Input : tuple = [('lucky', '18.265'), ('nikhil', '1
3 min read
Python | sort list of tuple based on sum Given, a list of tuple, the task is to sort the list of tuples based on the sum of elements in the tuple. Examples: Input: [(4, 5), (2, 3), (6, 7), (2, 8)] Output: [(2, 3), (4, 5), (2, 8), (6, 7)] Input: [(3, 4), (7, 8), (6, 5)] Output: [(3, 4), (6, 5), (7, 8)] # Method 1: Using bubble sort Using th
4 min read
How to iterate through list of tuples in Python In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Letâs explore these
2 min read
How to Sort a Set of Values in Python? Sorting means arranging the set of values in either an increasing or decreasing manner. There are various methods to sort values in Python. We can store a set or group of values using various data structures such as list, tuples, dictionaries which depends on the data we are storing. We can sort val
7 min read
Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read
Python | Sort Tuples in Increasing Order by any key Given a tuple, sort the list of tuples in increasing order by any key in tuple. Examples: Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)] m = 0 Output : [(1, 2), (2, 3), (2, 5), (4, 4)] Explanation: Sorted using the 0th index key. Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)] m = 2 Output : So
3 min read