Editor

Sort Words by Length

#228 · Python idioms · Beginner · 30 XP · about 5 min

Write sort_by_length(words) that returns a new list of the words sorted from shortest to longest. Words of the same length go in alphabetical order, and repeated words stay in the list.

Leave the original list unchanged.

Examples

sort_by_length(['banana', 'fig', 'apple', 'kiwi', 'date'])
# returns ['fig', 'date', 'kiwi', 'apple', 'banana']
sort_by_length(['bb', 'a', 'ccc', 'aa'])  # returns ['a', 'aa', 'bb', 'ccc']

In the first example "date" and "kiwi" both have 4 letters, so "date" comes first.

Note

Every word is written in lowercase letters.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.