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 lessonGiven 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
- Count Character FrequencyBeginner
- Group Names by First LetterIntermediate
- Predict the deque OutputBeginner
- Build a Recent History TrackerIntermediate
- Create and Use a namedtupleIntermediate
- Fix the defaultdict BugAdvanced
All Python exercises · 50 practice problems · Standard Library Essentials quiz