Python Practice › Tuple Programs › Program 29

Sort tuples by the first item

EasyTuple Programs · 29 of 70

Write a Python program to sort a list of tuples by their first item.

Advertisement
Input
[(1, 5), (7, 8), (4, 0), (3, 6)]
Output
[(1, 5), (3, 6), (4, 0), (7, 8)]

Approach

  1. Tuples sort by their first item by default.

Solution

Python
print(sorted([(1, 5), (7, 8), (4, 0), (3, 6)]))
Output
[(1, 5), (3, 6), (4, 0), (7, 8)]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs