Skip to content
Related Articles

Related Articles

Python – Dictionary List Values Frequency

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 31 Aug, 2021

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the task of computing frequency of all the values in dictionary values lists. This is quite common problem and can have use cases in many domains. Let’s discuss certain ways in which this task can be performed.
 

Input : test_dict = {1: [‘gfg’, ‘CS’, ‘cool’], 2: [‘gfg’, ‘CS’]} 
Output : {‘gfg’: 2, ‘CS’: 2, ‘cool’: 1}
Input : test_dict = {1 : [‘gfg’, ‘CS’]} 
Output : {‘gfg’: 1, ‘CS’: 1} 
 

Method #1 : Using defaultdict() + loop 
The combination of above functions can be used to solve this problem. In this, we use defaultdict() to initialize the counter for each value and brute force is used to increment the counter in dictionary lists.
 

Python3




# Python3 code to demonstrate working of
# Dictionary List Values Frequency
# Using loop + defaultdict()
from collections import defaultdict
 
# initializing dictionary
test_dict = {1 : ['gfg', 'best', 'geeks'],
             2 : ['gfg', 'CS'],
             3 : ['best', 'for', 'CS'],
             4 : ['test', 'ide', 'success'],
             5 : ['gfg', 'is', 'best']}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Dictionary List Values Frequency
# Using loop + defaultdict()
res = defaultdict(int)
for key, val in test_dict.items():
    for sub in val:
        res[sub] += 1
     
# printing result
print("Values Frequency : " + str(dict(res)))

Output : 

The original dictionary : {1: [‘gfg’, ‘best’, ‘geeks’], 2: [‘gfg’, ‘CS’], 3: [‘best’, ‘for’, ‘CS’], 4: [‘test’, ‘ide’, ‘success’], 5: [‘gfg’, ‘is’, ‘best’]} 
Values Frequency : {‘gfg’: 3, ‘best’: 3, ‘geeks’: 1, ‘CS’: 2, ‘for’: 1, ‘test’: 1, ‘ide’: 1, ‘success’: 1, ‘is’: 1} 

 

 
Method #2 : Using chain.from_iterables() + Counter() 
The combination of above functions can be used to solve this problem. In this, we perform task of Counting using Counter() and the flattening/extraction of values is done using from_iterables().
 

Python3




# Python3 code to demonstrate working of
# Dictionary List Values Frequency
# Using chain.from_iterables() + Counter()
from collections import Counter
from itertools import chain
 
# initializing dictionary
test_dict = {1 : ['gfg', 'best', 'geeks'],
             2 : ['gfg', 'CS'],
             3 : ['best', 'for', 'CS'],
             4 : ['test', 'ide', 'success'],
             5 : ['gfg', 'is', 'best']}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Dictionary List Values Frequency
# Using chain.from_iterables() + Counter()
res = Counter(chain.from_iterable(test_dict.values()))
     
# printing result
print("Values Frequency : " + str(dict(res)))

Output : 

The original dictionary : {1: [‘gfg’, ‘best’, ‘geeks’], 2: [‘gfg’, ‘CS’], 3: [‘best’, ‘for’, ‘CS’], 4: [‘test’, ‘ide’, ‘success’], 5: [‘gfg’, ‘is’, ‘best’]} 
Values Frequency : {‘gfg’: 3, ‘best’: 3, ‘geeks’: 1, ‘CS’: 2, ‘for’: 1, ‘test’: 1, ‘ide’: 1, ‘success’: 1, ‘is’: 1} 

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!