Python Program to Count Frequency of Words in String
Use
collections.Counter with split() to count word frequency like this: from collections import Counter; counts = Counter(your_string.split()).Examples
Inputhello world hello
Output{'hello': 2, 'world': 1}
Inputapple banana apple orange banana apple
Output{'apple': 3, 'banana': 2, 'orange': 1}
Input
Output{}
How to Think About It
To count how many times each word appears, first split the string into words using spaces. Then, for each word, keep track of how many times it shows up by adding one each time you see it. This way, you get a list of words with their counts.
Algorithm
1
Get the input string.2
Split the string into words using spaces.3
Create an empty dictionary to store word counts.4
For each word in the list, increase its count in the dictionary by one.5
Return or print the dictionary with word frequencies.Code
python
from collections import Counter def count_word_frequency(text): words = text.split() frequency = Counter(words) return dict(frequency) # Example usage input_text = "hello world hello" print(count_word_frequency(input_text))
Output
{'hello': 2, 'world': 1}
Dry Run
Let's trace the input 'hello world hello' through the code
1
Split the string
words = ['hello', 'world', 'hello']
2
Count words using Counter
frequency = Counter({'hello': 2, 'world': 1})
3
Convert Counter to dictionary
result = {'hello': 2, 'world': 1}
| Word | Count |
|---|---|
| hello | 2 |
| world | 1 |
Why This Works
Step 1: Splitting the string
Using split() breaks the string into a list of words separated by spaces.
Step 2: Counting words
Counter automatically counts how many times each word appears in the list.
Step 3: Returning the result
Converting the Counter to a dictionary makes it easy to read and use the word counts.
Alternative Approaches
Manual dictionary counting
python
def count_word_frequency_manual(text): words = text.split() frequency = {} for word in words: if word in frequency: frequency[word] += 1 else: frequency[word] = 1 return frequency print(count_word_frequency_manual('hello world hello'))
This method uses basic dictionary operations without importing modules, but is longer and less concise.
Using defaultdict from collections
python
from collections import defaultdict def count_word_frequency_defaultdict(text): words = text.split() frequency = defaultdict(int) for word in words: frequency[word] += 1 return dict(frequency) print(count_word_frequency_defaultdict('hello world hello'))
This approach avoids checking if a word exists in the dictionary by using defaultdict, making code cleaner.
Complexity: O(n) time, O(k) space
Time Complexity
The program processes each word once, so the time grows linearly with the number of words, O(n).
Space Complexity
It stores counts for each unique word, so space depends on the number of unique words, O(k).
Which Approach is Fastest?
Using Counter is fastest and most readable; manual counting is slower and more code.
| Approach | Time | Space | Best For |
|---|---|---|---|
| collections.Counter | O(n) | O(k) | Simple and fast counting |
| Manual dictionary | O(n) | O(k) | No imports, basic Python |
| defaultdict | O(n) | O(k) | Cleaner manual counting |
Use
collections.Counter for a simple and efficient way to count word frequencies.Beginners often forget to split the string into words before counting, causing incorrect results.