Editor

Sort Words by Length

Intermediate · Write the code · from Python Sorting: sort(), sorted(), Key Functions, and Custom Order

From the lesson: Python Sorting: sort(), sorted(), Key Functions, and Custom Order

Imagine your bookshelf at home. Books are scattered everywhere - some by title, some by color, some shoved in sideways. Finding anything takes forever.

Read the lesson

Sort the list of words by their length (shortest first). If two words have the same length, they should stay in their original order.

Print the sorted list.

words = ['elephant', 'cat', 'giraffe', 'ox', 'bear', 'ant']
# Sort by length and print

More Sorting exercises

All Python exercises · 50 practice problems · Data Structures quiz