How to Count Word Frequencies in Python
Count how often each word appears in a string and list the unique words using Python dictionaries and sets.
Python code
14 linesdef text_processor(text):
words = text.lower().split()
word_count = {}
for word in words:
word_count[word] = word_count.get(word, 0) + 1
unique_words = set(words)
return word_count, unique_words
if __name__ == "__main__":
sample_text = "The quick brown fox jumps over the lazy dog and the quick cat"
word_counts, unique = text_processor(sample_text)
print("Word counts:", word_counts)
print("Unique words:", sorted(unique))
print("Number of unique words:", len(unique))
Output
Word counts: {'the': 2, 'quick': 2, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1, 'and': 1, 'cat': 1}
Unique words: ['and', 'brown', 'cat', 'dog', 'fox', 'jumps', 'lazy', 'over', 'quick', 'the']
Number of unique words: 10
How it works
The function splits the lowercased text on whitespace with .split(), producing a list of words. It then loops over each word and uses dict.get(word, 0) to safely increment the count, avoiding a KeyError for new words. A set (set(words)) automatically removes duplicates, giving all unique words in O(n) time. Sorting unique alphabetically with sorted() makes the output easier to read. This pattern is fast and uses only built-in data structures.
Common mistakes
- Forgetting to call `.lower()` so 'The' and 'the' count as different words.
- Using `word_count[word] += 1` without checking if the key exists, raising a KeyError.
- Assuming `set(words)` preserves insertion order (it doesn't; use `dict` if order matters).
Variations
- Use `collections.Counter(text.split())` for a one-liner that returns a Counter with counts and unique keys.
- Strip punctuation with `re.findall(r'\b\w+\b', text.lower())` to ignore commas and periods.
Real-world use cases
- Analyzing customer feedback or survey responses to see which words appear most often.
- Building a simple search index that tracks term frequency per document.
- Detecting repeated phrases or jargon in support tickets for trend analysis.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.