Python Practice › Tuple Programs › Program 29
Sort tuples by the first item
Write a Python program to sort a list of tuples by their first item.
Input
[(1, 5), (7, 8), (4, 0), (3, 6)]
Output
[(1, 5), (3, 6), (4, 0), (7, 8)]
Approach
- Tuples sort by their first item by default.
Solution
print(sorted([(1, 5), (7, 8), (4, 0), (3, 6)]))Output
[(1, 5), (3, 6), (4, 0), (7, 8)]
Learn the concept
New to this topic? Read the lesson, then come back.