-4

What is the best way to find the total number of each value in dictionary variable?

This is my dictionary variable.

my_dict = {
        'mike':'football',
        'jack':'basketball',
        'tom':'basketball',
        'keanu':'basketball',
        'jason':'football'
}

And this is the output that i want.

Total number of each value

football : 2
basketball : 3 

Kaow
  • 483
  • 2
  • 9
  • 22
  • See more details in https://stackoverflow.com/questions/48371856 and https://stackoverflow.com/questions/14743454 – heilala Jul 23 '20 at 11:07

1 Answers1

2

You could use collections.Counter:

from collections import Counter

my_dict = {
        'mike':'football',
        'jack':'basketball',
        'tom':'basketball',
        'keanu':'basketball',
        'jason':'football'
}
print(Counter(my_dict.values()))

Result:

Counter({'basketball': 3, 'football': 2})
Kevin Mayo
  • 1,089
  • 6
  • 19