VOOZH about

URL: https://www.geeksforgeeks.org/python/counting-word-frequency-and-making-a-dictionary-from-it/

⇱ Counting Word Frequency and Making a Dictionary from it - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Counting Word Frequency and Making a Dictionary from it

Last Updated : 23 Jul, 2025

We need to count how often each word appears in a given text and store these counts in a dictionary. For instance, if the input string is "Python with Python gfg with Python", we want the output to be {'Python': 3, 'with': 2, 'gfg': 1}. Each key in the dictionary represents a unique word, and the corresponding value indicates its frequency. Below, we will discuss multiple methods to achieve this

Using collections.Counter

This method uses the Counter class from the collections module, which is specifically designed to count hashable objects like words.


Output
{'Python': 3, 'with': 2, 'gfg': 1}

Explanation:

  • The split() method divides the input string into individual words.
  • Counter processes the list of words and counts how many times each word appears.

Let's explore some more ways and see how count the word frequency and make a dictionary from it.

Using dictionary with loop

This method manually counts word frequencies by iterating through the list of words and updating a dictionary.


Output
{'Python': 3, 'with': 2, 'gfg': 1}

Explanation:

  • split() method divides the string into a list of words.
  • The for loop iterates over each word in the list.
  • The get method initializes the count to 0 if the word is not already in the dictionary, and then increments the count.

Using dictionary comprehension

This method uses dictionary comprehension to build a frequency dictionary by iterating over the unique words in the input string. It is concise but less efficient due to repeated calls to the count() method for each unique word.


Output
{'with': 2, 'Python': 3, 'gfg': 1}

Explanation:

  • split() method creates a list of words, and set ensures that we only process unique words.
  • comprehension iterates over each unique word and uses the count() method to calculate its frequency.

Using pandas.value_counts

This method uses the pandas library, which provides efficient tools for data manipulation. It is particularly useful when working with large datasets.


Output
{'Python': 3, 'with': 2, 'gfg': 1}

Explanation:

  • split() method creates a list of words.
  • value_counts function from pandas efficiently counts occurrences of each unique word.

Using reduce from functools

This method uses the reduce() function to build the frequency dictionary by processing words one by one.


Output
{'Python': 3, 'with': 2, 'gfg': 1}

Explanation:

  • split() method creates a list of words.
  • reduce() function iterates over the list, updating the dictionary for each word.
  • update() method increments the word count or initializes it to 1 if the word is not already in the dictionary.
Comment
Article Tags:
Article Tags: