Python Practice › Tuple Programs › Program 30
Sort tuples by the second item
Write a Python program to sort a list of tuples by their second item.
Input
[(1, 5), (7, 8), (4, 0), (3, 6)]
Output
[(4, 0), (1, 5), (3, 6), (7, 8)]
Approach
key=lambda t: t[1].
Solution
data = [(1, 5), (7, 8), (4, 0), (3, 6)]print(sorted(data, key=lambda t: t[1]))Output
[(4, 0), (1, 5), (3, 6), (7, 8)]
Learn the concept
New to this topic? Read the lesson, then come back.