Open In App

Custom Sorting in List of Tuples – Python

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 the first element in descending order and then by the second element in ascending order would result in [(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)].

Using sorted()

sorted() is commonly used for custom sorting when a new sorted list is needed without modifying the original. It allows the use of a key argument with a lambda function to define custom sorting logic . It is ideal when sorting tuples based on descending order of the first element and ascending order of the second element.

Python
a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]

res = sorted(a, key=lambda x: (-x[0], x[1]))
print(res)

Output
[(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)]

Explanation: sorted(a, key=lambda x: (-x[0], x[1])) sorts the list of tuples a by the first element in descending order and if two tuples have the same first element, by the second element in ascending order.

Using sort()

sort() is similar to sorted() but sorts the list in-place, modifying the original list. It is efficient when us do not need to preserve the original list. For custom sorting can be applied using a lambda expression as the key parameter, useful for sorting tuples with different sorting orders for elements.

Python
a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]

a.sort(key=lambda x: (-x[0], x[1]))
print(a)

Output
[(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)]

Explanation: a.sort(key=lambda x: (-x[0], x[1])) sorts the list of tuples a in place by the first element in descending order and if two tuples have the same first element, by the second element in ascending order. Unlike sorted(), sort() modifies the original list.

Using itemgetter()

itemgetter() from the operator module is a faster alternative to lambda when sorting by tuple elements. It is often used for sorting large datasets. For custom sorting e.g., descending by the first element and ascending by the second, it can be combined with multiple sort calls.

Python
from operator import itemgetter

a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]

a.sort(key=itemgetter(1))
a.sort(key=itemgetter(0), reverse=True)
print(a)

Output
[(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)]

Explanation: a.sort(key=itemgetter(1)) orders tuples by the second element in ascending order and a.sort(key=itemgetter(0), reverse=True) orders tuples by the first element in descending order, while preserving the previous order for equal first elements .

Using functools.cmp_to_key()

cmp_to_key() from functools allows using a custom comparator function instead of a key. This is useful when sorting requires complex comparison logic. It is particularly helpful when sorting tuples based on multiple fields with different sort orders, such as descending by the first and ascending by the second.

Python
from functools import cmp_to_key
a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)]

res = sorted(a, key=cmp_to_key(lambda a, b: b[0] - a[0] if a[0] != b[0] else a[1] - b[1]))
print(res)

Output
[(10, 1), (10, 4), (7, 5), (7, 8), (5, 6)]

Explanation: cmp_to_key() first sorts by the first element in descending order. If the first elements are equal, it sorts by the second element in ascending order.



Similar Reads