How to Count Words and Find Common Words in Python with Dictionaries and Sets

Build a simple text processor that counts unique words with dictionaries and finds common words across text halves using sets.

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

Python code

28 lines
Python 3.9+
def process_text(text):
    """Process text: count unique words with counts, find common words."""
    words = text.lower().replace(",", "").replace(".", "").split()
    
    word_counts = {}
    for word in words:
        word_counts[word] = word_counts.get(word, 0) + 1
    
    total_words = len(words)
    unique_words = len(word_counts)
    
    first_set = set(words[:len(words)//2])
    second_set = set(words[len(words)//2:])
    common_words = first_set & second_set
    all_words = first_set | second_set
    
    print(f"Total words: {total_words}")
    print(f"Unique words: {unique_words}")
    print(f"Word counts: {word_counts}")
    print(f"Common words in halves: {sorted(common_words)}")
    print(f"All unique words: {sorted(all_words)}")
    print(f"Most frequent word: {max(word_counts, key=word_counts.get)}")
    
    return word_counts

if __name__ == "__main__":
    sample_text = "Python is fun. Python is powerful. Learning Python is exciting."
    process_text(sample_text)

Output

stdout
Total words: 9
Unique words: 5
Word counts: {'python': 3, 'is': 2, 'fun': 1, 'powerful': 1, 'exciting': 1}
Common words in halves: ['is']
All unique words: ['exciting', 'fun', 'is', 'powerful', 'python']
Most frequent word: python

How it works

The lower() method normalizes all text to lowercase so 'Python' and 'python' count as the same word. A dictionary accumulates counts using get(word, 0) + 1 to avoid a KeyError on new words. Splitting the word list in half and converting each half to a set lets & (intersection) find words appearing in both halves, while | (union) collects all unique words. The max function with key=word_counts.get returns the word with the highest count. This pattern efficiently combines the strengths of dictionaries for counting and sets for set operations.

Common mistakes

  • Forgetting to lowercase text, causing 'Python' and 'python' to be counted separately
  • Not removing punctuation like commas or periods, skewing word counts
  • Assuming a word is in the dictionary and using direct indexing instead of `get()`

Variations

  1. Use `collections.Counter` to count words in one line: `Counter(words)`
  2. Use a regular expression `re.findall(r'\b\w+\b', text.lower())` to split words more robustly

Real-world use cases

  • Analyzing customer feedback to find the most mentioned features or complaints.
  • Building a simple search index that tracks word frequency across documents.
  • Comparing two halves of a log file to spot anomalies in repeated error messages.

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.