Python - Multiple Column Sort in Tuples

In Python, sorting tuples by multiple columns means organizing data based on several criteria in priority order. Python provides elegant solutions using the sorted() function with custom sorting keys.

Understanding Multiple Column Sorting

When sorting tuples by multiple columns, Python first sorts by the first specified column, then by the second column for ties, and so on. Each tuple represents a row of data ?

# Example: Sort by first column, then by second column
data = [(5, 8), (4, 6), (2, 5), (7, 9), (5, 3)]
sorted_data = sorted(data)
print("Original:", data)
print("Sorted:", sorted_data)
Original: [(5, 8), (4, 6), (2, 5), (7, 9), (5, 3)]
Sorted: [(2, 5), (4, 6), (5, 3), (5, 8), (7, 9)]

Using Lambda Function

A lambda function creates a custom sorting key that extracts values from specified columns ?

data = [
    (6, 2, 'apple'),
    (3, 2, 'banana'),
    (6, 2, 'cherry'),
    (4, 3, 'apple'),
    (3, 2, 'date'),
    (1, 2, 'orange'),
]

# Sort by column 0, then by column 1
sorted_data = sorted(data, key=lambda x: (x[0], x[1]))

for item in sorted_data:
    print(item)
(1, 2, 'orange')
(3, 2, 'banana')
(3, 2, 'date')
(4, 3, 'apple')
(6, 2, 'apple')
(6, 2, 'cherry')

Using operator.itemgetter

The itemgetter function provides a cleaner alternative for extracting multiple indices ?

from operator import itemgetter

data = [
    (6, 2, 'apple'),
    (3, 2, 'banana'),
    (6, 2, 'cherry'),
    (4, 3, 'apple'),
    (3, 2, 'date'),
    (1, 2, 'orange'),
]

# Sort by column 0, then by column 1
sorted_data = sorted(data, key=itemgetter(0, 1))

for item in sorted_data:
    print(item)
(1, 2, 'orange')
(3, 2, 'banana')
(3, 2, 'date')
(4, 3, 'apple')
(6, 2, 'apple')
(6, 2, 'cherry')

Reverse Sorting

You can sort in descending order using the reverse parameter ?

from operator import itemgetter

scores = [
    ('Alice', 85, 92),
    ('Bob', 78, 88),
    ('Charlie', 85, 95),
    ('Diana', 90, 88)
]

# Sort by score (descending), then by bonus (descending)
sorted_scores = sorted(scores, key=itemgetter(1, 2), reverse=True)

for student in sorted_scores:
    print(f"{student[0]}: Score={student[1]}, Bonus={student[2]}")
Diana: Score=90, Bonus=88
Charlie: Score=85, Bonus=95
Alice: Score=85, Bonus=92
Bob: Score=78, Bonus=88

Comparison

Method Readability Performance Best For
Lambda function Good Slightly slower Complex sorting logic
itemgetter Excellent Faster Simple column extraction

Conclusion

Use itemgetter for clean, efficient multiple column sorting. Both approaches have O(n log n) time complexity, but itemgetter is more readable and performs better for simple column-based sorting.

Updated on: 2026-03-27T15:31:49+05:30

618 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements