# Python – Words Frequency in String Shorthands

Sometimes while working with Python strings, we can have a problem in which we need to extract the frequency of all the words in a string. This problem has been solved earlier. This discusses the shorthands to solve this problem as this has applications in many domains ranging from web development and competitive programming. Let’s discuss certain ways in which this problem can be solved.

```Input : test_str = 'Gfg is best'
Output : {'Gfg': 1, 'is': 1, 'best': 1} ```
```Input : test_str = 'Gfg Gfg Gfg'
Output : {'Gfg': 3}```

Method #1: Using dictionary comprehension + count() + split()

The combination of the above functions can be used to solve this problem. In this, we first split all the words and then perform a count of them using count() method.

## Python3

 `# Python3 code to demonstrate working of` `# Words Frequency in String Shorthands` `# Using dictionary comprehension + count() + split()`   `# Initializing string` `test_str ``=` `'Gfg is best . Geeks are good and Geeks like Gfg'`   `# Printing original string` `print``(``"The original string is : "` `+` `str``(test_str))`   `# Words Frequency in String Shorthands` `# Using dictionary comprehension + count() + split()` `res ``=` `{key: test_str.count(key) ``for` `key ``in` `test_str.split()}`   `# Printing result` `print``(``"The words frequency : "` `+` `str``(res))`

Output :

The original string is : Gfg is best . Geeks are good and Geeks like Gfg The words frequency : {‘Gfg’: 2, ‘is’: 1, ‘best’: 1, ‘.’: 1, ‘Geeks’: 2, ‘are’: 1, ‘good’: 1, ‘and’: 1, ‘like’: 1}

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using Counter() + split()

The combination of the above methods can also be used to solve this problem. In this, we perform the task of counting using Counter() and separation of words using split().

## Python3

 `# Python3 code to demonstrate working of` `# Words Frequency in String Shorthands` `# Using Counter() + split()`   `from` `collections ``import` `Counter`   `# initializing string` `test_str ``=` `'Gfg is best . Geeks are good and Geeks like Gfg'`   `# printing original string` `print``(``"The original string is : "` `+` `str``(test_str))`   `# Words Frequency in String Shorthands` `# using Counter() + split()` `res ``=` `Counter(test_str.split())`   `# Printing result` `print``(``"The words frequency : "` `+` `str``(``dict``(res)))`

Output :

The original string is : Gfg is best . Geeks are good and Geeks like Gfg The words frequency : {‘Gfg’: 2, ‘is’: 1, ‘best’: 1, ‘.’: 1, ‘Geeks’: 2, ‘are’: 1, ‘good’: 1, ‘and’: 1, ‘like’: 1}

Method #3 : Using dictionary comprehension + operator.countOf() + split() :

The combination of the above functions can be used to solve this problem. In this, we first split all the words and then perform count of them using operator.countOf() method.

## Python3

 `# Python3 code to demonstrate working of` `# Words Frequency in String Shorthands` `# Using dictionary comprehension + operator.countOf() + split()`   `import` `operator as op`   `# Initializing string` `test_str ``=` `'Gfg is best . Geeks are good and Geeks like Gfg'`   `# Printing original string` `print``(``"The original string is : "` `+` `str``(test_str))` `listString ``=` `test_str.split()`   `# Words Frequency in String Shorthands` `# Using dictionary comprehension + operator.countOf()` `res ``=` `{key: op.countOf(listString, key) ``for` `key ``in` `listString}`   `# Printing the result` `print``(``"The words frequency : "` `+` `str``(res))`

Output

```The original string is : Gfg is best . Geeks are good and Geeks like Gfg
The words frequency : {'Gfg': 2, 'is': 1, 'best': 1, '.': 1, 'Geeks': 2, 'are': 1, 'good': 1, 'and': 1, 'like': 1}```

Time Complexity: O(N)
Auxiliary Space : O(N)

Method #4: Using defaultdict

## Python3

 `from` `collections ``import` `defaultdict`   `# Initializing string` `test_str ``=` `'Gfg is best . Geeks are good and Geeks like Gfg'`   `# Printing original string` `print``(``"The original string is : "` `+` `str``(test_str))`   `# Split the string into a list of words` `listString ``=` `test_str.split()`   `# Creating a defaultdict with default value 0` `freq ``=` `defaultdict(``int``)`   `# Iterating through the list of words and` `# count the frequency of each word` `for` `word ``in` `listString:` `    ``freq[word] ``+``=` `1`   `# Converting the defaultdict to a regular dictionary` `res ``=` `dict``(freq)`   `# Printing result` `print``(``"The words frequency : "` `+` `str``(res))`

Output

```The original string is : Gfg is best . Geeks are good and Geeks like Gfg
The words frequency : {'Gfg': 2, 'is': 1, 'best': 1, '.': 1, 'Geeks': 2, 'are': 1, 'good': 1, 'and': 1, 'like': 1}```

Time Complexity: O(n), where n is the number of words in the string.
Auxiliary Space: O(n), where n is the number of unique words in the string.

Method #5: Using set() and list comprehension

Step-by-step approach:

• Split the string into a list of words.
• Use set() to get a unique set of words.
• Use a list comprehension to count the frequency of each word in the original list.
• Store the results in a dictionary using dictionary comprehension.
• Print the final result.

## Python3

 `# Initializing string` `test_str ``=` `'Gfg is best . Geeks are good and Geeks like Gfg'`   `# Printing original string` `print``(``"The original string is : "` `+` `str``(test_str))`   `# Split the string into a list of words` `listString ``=` `test_str.split()`   `# Using set() and list comprehension to count the frequency of each word` `freq ``=` `{word: listString.count(word) ``for` `word ``in` `set``(listString)}`   `# Printing result` `print``(``"The words frequency : "` `+` `str``(freq))`

Output

```The original string is : Gfg is best . Geeks are good and Geeks like Gfg
The words frequency : {'are': 1, 'good': 1, 'and': 1, 'like': 1, 'best': 1, 'Gfg': 2, 'is': 1, 'Geeks': 2, '.': 1}```

Time complexity: O(n^2) where n is the length of the list of words.
Auxiliary space: O(n) where n is the length of the list of words.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next