Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple - Python
Last Updated :
19 Feb, 2025
The task of sorting a list of tuples in Python based on the last element of each tuple is a common task when working with structured data. This involves arranging the tuples in increasing order according to their second or last value, often for easier data analysis or searching. For example, given a list [(1, 3), (3, 2), (2, 1)], sorting by the last element will result in [(2, 1), (3, 2), (1, 3)].
Using sorted()
sorted() is the most efficient way to sort a list of tuples. It allows the use of a key argument with a lambda function to specify that sorting should be done based on the last element of each tuple.
Python
a = [(1, 3), (3, 2), (2, 1)]
res = sorted(a, key=lambda x: x[1])
print(res)
Output[(2, 1), (3, 2), (1, 3)]
Explanation : sorted() with key=lambda x: x[1] sorts tuples by their second element and sorted list is stored in res .
Using sort()
sort() works similarly to sorted(), but it modifies the original list in place instead of returning a new one. It’s slightly more memory-efficient because it doesn't create a copy.
Python
a = [(1, 3), (3, 2), (2, 1)]
a.sort(key=lambda x: x[1])
print(a)
Output[(2, 1), (3, 2), (1, 3)]
Explanation:sort() with key=lambda x: x[1] sorts the list a in place based on the second element of each tuple. It modifies the original list instead of creating a new one.
Using itemgetter()
itemgetter() from the operator module can be used instead of a lambda function. It is faster for large datasets because it avoids function call overhead
Python
from operator import itemgetter
a = [(1, 3), (3, 2), (2, 1)]
res = sorted(a, key=itemgetter(1))
print(res)
Output[(2, 1), (3, 2), (1, 3)]
Explanation: sorted() with key=itemgetter(1) sorts tuples by their second element. itemgetter(1) efficiently retrieves the second element and the sorted result is stored in res.
Using list comprehension
While less common and less readable, list comprehension with enumerate() can also achieve sorting by the last element. This is more of a creative approach, often useful if we need tuple indices as part of the sorting result.
Python
a = [(1, 3), (3, 2), (2, 1)]
res = [x for _, x in sorted(enumerate(a), key=lambda x: x[1][1])]
print(res)
Output[(2, 1), (3, 2), (1, 3)]
Explanation: enumerate(a) pairs tuples with indices, sorts by the second element using key=lambda x: x[1][1] and list comprehension extracts the sorted tuples into res.
Similar Reads
Python - Sort Tuple List by Nth Element of Tuple We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)]Using sorted() with lambdasorted() function wit
3 min read
Python - Sort Tuple List by Nth Element of Tuple We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)]Using sorted() with lambdasorted() function wit
3 min read
Sorting List of Lists with First Element of Each Sub-List in Python In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list o
3 min read
Sort List of Lists by Lexicographic Value and then Length - Python In this problem, sorting a list of lists by lexicographic value and then length means arranging the sublists first by their natural order (lexicographically) and then by their size. For example: For the list [[3, 2], [1, 4, 6], [1, 2], [3, 2, 1]], sorting by lexicographic value and then by length wo
3 min read
Sort List of Lists by Lexicographic Value and then Length - Python In this problem, sorting a list of lists by lexicographic value and then length means arranging the sublists first by their natural order (lexicographically) and then by their size. For example: For the list [[3, 2], [1, 4, 6], [1, 2], [3, 2, 1]], sorting by lexicographic value and then by length wo
3 min read
Python | Reverse each tuple in a list of tuples Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega
5 min read