Deviopath › Python

Word Count

Easy · Dictionaries

Dicts are great for counting: counts[word] = counts.get(word, 0) + 1 adds 1, starting from 0 for new words. collections.Counter does the same job in one call.

Write a function word_count(text) that returns a dict mapping each word to how many times it appears. Split on whitespace and lowercase every word, so "The" and "the" count as the same word.

Next: Flatten a List of Lists