w3resource

Python Data Structure: Count the most common words in a dictionary

Python Data Structure: Exercise-5 with Solution

Write a Python program to count the most common words in a dictionary.

Sample Solution:

Python Code:

words = [
   'red', 'green', 'black', 'pink', 'black', 'white', 'black', 'eyes',
   'white', 'black', 'orange', 'pink', 'pink', 'red', 'red', 'white', 'orange',
   'white', "black", 'pink', 'green', 'green', 'pink', 'green', 'pink',
   'white', 'orange', "orange", 'red'
]
from collections import Counter
word_counts = Counter(words)
top_four = word_counts.most_common(4)
print(top_four)

Sample Output:

[('pink', 6), ('white', 5), ('black', 5), ('orange', 4)]

Flowchart:

Flowchart: Count the most common words in a dictionary

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to get all values from an enum class.
Next: Write a Python program to find the class wise roll number from a tuple-of-tuples.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/data-structures-and-algorithms/python-data-structure-exercise-5.php