w3resource

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


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.



Follow us on Facebook and Twitter for latest update.