Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Frequencies of Values in a Dictionary

Python – Frequencies of Values in a Dictionary

Sometimes, while working with python dictionaries, we can have a problem in which we need to extract the frequency of values in the dictionary. This is quite a common problem and has applications in many domains including web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘ide’ : 3, ‘Gfg’ : 3, ‘code’ : 2} 
Output : {3: 2, 2: 1} 

Input : test_dict = {10 : 1, 20 : 2, 30 : 1, 40 : 2 } 
Output : {1 : 2, 2 : 2}

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 dictionary with integers and increment counter in brute force manner using loop. 

Python3




# Python3 code to demonstrate working of
# Dictionary Values Frequency
# Using defaultdict() + loop
from collections import defaultdict
 
# initializing dictionary
test_dict = {'ide' : 3, 'Gfg' : 3, 'code' : 2}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Dictionary Values Frequency
# Using defaultdict() + loop
res = defaultdict(int)
for key, val in test_dict.items():
    res[val] += 1
     
# printing result
print("The frequency dictionary : " + str(dict(res)))


Output:

The original dictionary : {'Gfg': 3, 'code': 2, 'ide': 3}
The frequency dictionary : {2: 1, 3: 2}

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #2 : Using Counter() + values() The combination of above functions can be used to solve this problem. In this, we perform the task of extraction of values using values() and frequency counter using Counter(). 

Python3




# Python3 code to demonstrate working of
# Dictionary Values Frequency
# Using Counter() + values()
from collections import Counter
 
# initializing dictionary
test_dict = {'ide' : 3, 'Gfg' : 3, 'code' : 2}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Dictionary Values Frequency
# Using defaultdict() + loop
res = Counter(test_dict.values())
     
# printing result
print("The frequency dictionary : " + str(dict(res)))


Output:

The original dictionary : {'code': 2, 'Gfg': 3, 'ide': 3}
The frequency dictionary : {2: 1, 3: 2}

Method #3 : Using values(),count(),list(),set() methods

Approach

  1. Create an empty dictionary res, store the values of dictionary in a list x(list(),values())
  2. Store the unique values of a list x in list y(using list(),set())
  3. Initiate a for loop to traverse unique values list y
  4. Inside for loop with unique value as key and count of unique value in list y as value(using count())
  5. Display output dictionary res

Python3




# Python3 code to demonstrate working of
# Dictionary Values Frequency
 
# initializing dictionary
test_dict = {'ide' : 3, 'Gfg' : 3, 'code' : 2}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Dictionary Values Frequency
 
res=dict()
x=list(test_dict.values())   
y=list(set(x))
for i in y:
    res[i]=x.count(i)
# printing result
print("The frequency dictionary : " + str(res))


Output

The original dictionary : {'ide': 3, 'Gfg': 3, 'code': 2}
The frequency dictionary : {2: 1, 3: 2}

Time Complexity: O(N), where N is the length of dictionary values list
Auxiliary Space: O(N)

Method #4: Using values(),operator.countOf(),list(),set() methods

Step-by-step approach:

  1. Create an empty dictionary res, store the values of dictionary in a list x(list(),values())
  2. Store the unique values of a list x in list y(using list(),set())
  3. Initiate a for loop to traverse unique values list y
  4. Inside for loop with unique value as key and count of unique value in list y as value(using operator.countOf())
  5. Display output dictionary res

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Dictionary Values Frequency
 
# initializing dictionary
test_dict = {'ide' : 3, 'Gfg' : 3, 'code' : 2}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Dictionary Values Frequency
 
res=dict()
x=list(test_dict.values())   
y=list(set(x))
import operator
for i in y:
    res[i]=operator.countOf(x,i)
# printing result
print("The frequency dictionary : " + str(res))


Output

The original dictionary : {'ide': 3, 'Gfg': 3, 'code': 2}
The frequency dictionary : {2: 1, 3: 2}

Time Complexity: O(N), where N is the length of dictionary values list
Auxiliary Space: O(N)

RELATED ARTICLES

Most Popular

Recent Comments