Word Frequency Counter
Medium
Write a function word_frequency(text) that takes a string and returns a dictionary mapping each word (lowercase) to the number of times it appears.
Input
A string of space-separated words.
Output
A dictionary {word: count, ...}.
Example
Input
"apple banana apple orange banana apple"
Output
{'apple': 3, 'banana': 2, 'orange': 1}
Loop through the words, and for each word use dict.get(word, 0) + 1 to increment its count.
15 XP on solve
Back to lesson
Your Solution
Output
Click "Run" to execute your code...
⏳ Loading Python runtime (first run may take a few seconds)...
Log in to submit your solution and earn XP.