Python Practice › Tuple Programs › Program 31

Sort tuples by length

EasyTuple Programs · 31 of 70

Write a Python program to sort a list of tuples by length.

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

Approach

  1. key=len.

Solution

Python
print(sorted([(4, 5, 6), (6,), (2, 3), (6, 7, 8, 9, 0)], key=len))
Output
[(6,), (2, 3), (4, 5, 6), (6, 7, 8, 9, 0)]
Advertisement

Learn the concept

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

More tuple programs

← All 70 Tuple Programs