Sort tuples by the second item
Write a Python program to sort a list of tuples by their second item.
[(1, 5), (7, 8), (4, 0), (3, 6)]
[(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]))[(4, 0), (1, 5), (3, 6), (7, 8)]
Learn the concept
New to this topic? Read the lesson, then come back.