Editor

Word Lengths

#257 · Python idioms · Beginner · 40 XP · about 7 min

Write word_lengths(text) that returns a dictionary mapping each word of text to its length, in the order the words first appear.

Words are separated by whitespace. Words that differ only in upper and lower case count as the same word: it appears once, under the spelling it had the first time. So in 'Red red RED blue' the word red is stored once, as 'Red'.

Examples

word_lengths('the cat sat')         # returns {'the': 3, 'cat': 3, 'sat': 3}
word_lengths('Red red RED blue')    # returns {'Red': 3, 'blue': 4}
word_lengths('')                    # returns {}

Note

The text contains only letters and whitespace (spaces, tabs or new lines, possibly several in a row).

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.