Python Practice › Tuple Programs › Program 39
Sort tuples by a float element
Write a Python program to sort tuples by their float element.
Input
[(3, 5.6), (6, 2.3), (1, 1.8)]
Output
[(1, 1.8), (6, 2.3), (3, 5.6)]
Approach
key=lambda t: t[1].
Solution
print(sorted([(3, 5.6), (6, 2.3), (1, 1.8)], key=lambda t: t[1]))Output
[(1, 1.8), (6, 2.3), (3, 5.6)]
Learn the concept
New to this topic? Read the lesson, then come back.