Editor

Find the Top 2 Most Common Words

Beginner · Write the code · from Python collections: Counter, defaultdict, deque, and More

From the lesson: Python collections: Counter, defaultdict, deque, and More

Imagine you have a basic toolbox with a hammer and a screwdriver. They work fine for simple jobs. But when you're building a deck, you really want a power drill, a level, and a tape measure.

Read the lesson

Given the list words = ['cat', 'dog', 'cat', 'bird', 'dog', 'cat', 'fish'], use Counter to find and print the 2 most common words. Use most_common(2).

from collections import Counter
words = ['cat', 'dog', 'cat', 'bird', 'dog', 'cat', 'fish']
# Find and print the 2 most common words

More collections exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz