How to count words and find unique words in Python

Build a beginner-friendly text processor that counts word frequencies, finds unique words, and identifies words with vowels using dictionaries and sets.

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

Python code

20 lines
Python 3.9+
def text_processor(text):
    words = text.lower().replace(",", "").replace(".", "").split()
    word_count = {}
    
    for word in words:
        word_count[word] = word_count.get(word, 0) + 1
    
    unique_words = set(words)
    vowels = set("aeiou")
    words_with_vowels = {word for word in unique_words if vowels & set(word)}
    
    print(f"Total words: {len(words)}")
    print(f"Unique words: {len(unique_words)}")
    print(f"Most frequent words: {sorted(word_count.items(), key=lambda x: x[1], reverse=True)[:3]}")
    print(f"Words containing vowels: {sorted(words_with_vowels)[:5]}...")
    return word_count

if __name__ == "__main__":
    sample_text = "The quick brown fox jumps over the lazy dog. The dog sleeps."
    result = text_processor(sample_text)

Output

stdout
Total words: 11
Unique words: 9
Most frequent words: [('the', 3), ('dog', 2), ('quick', 1)]
Words containing vowels: ['brown', 'dog', 'fox', 'jumps', 'lazy']...

How it works

The text.lower().replace().split() chain normalizes text to lowercase, removes commas and periods, then splits into a list of words. A dictionary word_count stores each word as a key and its frequency as the value using get() to safely increment or initialize counts. The set(words) creates a collection of unique words, and set intersection (vowels & set(word)) checks whether a word contains any vowel characters. The f-string output formats results clearly, and sorted() with a lambda key sorts word-frequency pairs by count in descending order.

Common mistakes

  • Forgetting to strip punctuation before counting, leading to 'dog.' and 'dog' counted separately
  • Using a list instead of a set, causing duplicate words in unique-word calculations
  • Forgetting to lower() first, so 'The' and 'the' are treated as different words

Variations

  1. Use `collections.Counter(words)` instead of a manual dictionary loop
  2. Use a regex like `re.findall(r'\b\w+\b', text.lower())` for more robust punctuation handling

Real-world use cases

  • Analyzing customer feedback to surface frequently used terms for sentiment or trend reports.
  • Generating tag clouds or keyword summaries from blog posts or support ticket descriptions.
  • Building a search index feature that needs unique word lists and occurrence counts for ranking.

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.