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.

Easy Python 3.9+ Aug 9, 2026 Dictionaries & sets 14 views 0 copies

Python code

14 lines
Python 3.9+
def 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

stdout
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

  1. Use `collections.Counter(text.split())` for a one-liner that returns a Counter with counts and unique keys.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Dictionaries & sets

Related tutorials and quizzes for this topic.