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 lessonSort 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
- Sort a List of NumbersBeginner
- Predict the Output: sort() vs sorted()Beginner
- Fix the Bug: Case-Sensitive SortingIntermediate
- Sort Students by GradeIntermediate
- Sort by Multiple CriteriaAdvanced
All Python exercises · 50 practice problems · Data Structures quiz