w3resource

Python: Count the frequency in a given dictionary

Python dictionary: Exercise-61 with Solution

Write a Python program to count the frequency in a given dictionary.

Sample Solution:

Python Code:

from collections import Counter
def test(dictt):    
    result = Counter(dictt.values())
    return result    

dictt = {
 'V': 10,
 'VI': 10,
 'VII': 40,
 'VIII': 20,
 'IX': 70,
 'X': 80,
 'XI': 40,
 'XII': 20, 
 }

print("\nOriginal Dictionary:")
print(dictt)
print("\nCount the frequency of the said dictionary:")
print(test(dictt))

Sample Output:

Original Dictionary:
{'V': 10, 'VI': 10, 'VII': 40, 'VIII': 20, 'IX': 70, 'X': 80, 'XI': 40, 'XII': 20}

Count the frequency of the said dictionary:
Counter({10: 2, 40: 2, 20: 2, 70: 1, 80: 1})

Pictorial Presentation:

Python Dictionary: Count the frequency in a given dictionary.

Flowchart:

Flowchart: Count the frequency in a given dictionary.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find shortest list of values with the keys in a given dictionary
Next: Write a Python program to extract values from a given dictionaries and create a list of lists from those values.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()