← Back to challenges

Sort a List by String Length

PythonHardarrayssortingalgorithms

Instructions

Create a function that takes a list of strings and return a list, sorted from shortest to longest.

Examples

sort_by_length(["Google", "Apple", "Microsoft"])
➞ ["Apple", "Google", "Microsoft"]

sort_by_length(["Leonardo", "Michelangelo", "Raphael", "Donatello"])
➞ ["Raphael", "Leonardo", "Donatello", "Michelangelo"]

sort_by_length(["Turing", "Einstein", "Jung"])
➞ ["Jung", "Turing", "Einstein"]

Notes

All test cases contain lists with strings of different lengths, so you won't have to deal with multiple strings of the same length.

python3
Loading editor…
⌘ ↵ to run
Walks through the solution with reasoning and edge cases.