Python Practice › Tuple Programs › Program 39

Sort tuples by a float element

EasyTuple Programs · 39 of 70

Write a Python program to sort tuples by their float element.

Advertisement
Input
[(3, 5.6), (6, 2.3), (1, 1.8)]
Output
[(1, 1.8), (6, 2.3), (3, 5.6)]

Approach

  1. key=lambda t: t[1].

Solution

Python
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)]
Advertisement

Learn the concept

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

More tuple programs

← All 70 Tuple Programs