Python Practice › Tuple Programs › Program 31
Sort tuples by length
Write a Python program to sort a list of tuples by length.
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
key=len.
Solution
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)]
Learn the concept
New to this topic? Read the lesson, then come back.