Skip to content
Code ReadingSorting by a Key
Predict the Output

Sorting by a Key

Sorted by length first, then alphabetically on ties. What are the first three words?
1words = ["kiwi", "fig", "banana", "apple", "date"]
2print(sorted(words, key=lambda w: (len(w), w))[:3])