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.
Python code
20 linesdef 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
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
- Use `collections.Counter(words)` instead of a manual dictionary loop
- 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
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.