Open In App

Python – Frequency Grouping Dictionary

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the grouping of dictionary data, in a way in which we need to group all the similar dictionaries key with its frequency. This kind of problem has its application in web development domain. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [{‘best’: 2, ‘Gfg’: 1}, {‘best’: 2, ‘Gfg’: 1}] 
Output : [{‘freq’: 2, ‘Gfg’: 1}] 

Input : test_list = [{‘Gfg’: 1, ‘best’: 2}, {‘Gfg’: 2, ‘best’: 1}] 
Output : [{‘Gfg’: 1, ‘freq’: 1}, {‘Gfg’: 2, ‘freq’: 1}]

Method #1 : Using defaultdict() + list comprehension The combination of above functions can be used to perform this task. In this, we initialize the defaultdict with integer to get the frequency and list comprehension is used to compile the resultant dictionary. 

Python3




# Python3 code to demonstrate working of
# Frequency Grouping Dictionary
# Using defaultdict() + list comprehension
from collections import defaultdict
 
# initializing list
test_list = [{'Gfg' 1, 'best' : 2},
             {'Gfg' 1, 'best' : 2},
             {'Gfg' 2, 'good' : 3},
             {'Gfg' 2, 'best' : 2},
             {'Gfg' 2, 'good' : 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Frequency Grouping Dictionary
# Using defaultdict() + list comprehension
temp = defaultdict(int)
for sub in test_list:
    key = sub['Gfg']
    temp[key] += 1
 
res = [{"Gfg": key, "freq": val} for (key, val) in temp.items()]
 
# printing result
print("The frequency dictionary : " + str(res))


Output : 

The original list is : [{‘Gfg’: 1, ‘best’: 2}, {‘Gfg’: 1, ‘best’: 2}, {‘good’: 3, ‘Gfg’: 2}, {‘Gfg’: 2, ‘best’: 2}, {‘good’: 3, ‘Gfg’: 2}] The frequency dictionary : [{‘Gfg’: 1, ‘freq’: 2}, {‘Gfg’: 2, ‘freq’: 3}]

Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), since we are using a defaultdict to store the frequency of each element in the input list, and the resulting list also has n elements.

Method #2: Using a regular dictionary and a for loop

Step-by-step explanation:

We initialize the test_list with the same values as in Method #1.
We print the original list using the print() function and str() function to convert the list to a string.
We create an empty dictionary called temp.
We iterate through each element in test_list using a for loop.
We extract the value associated with the key ‘Gfg’ in the current element using the sub[‘Gfg’] syntax.
We check if the current key is already in the temp dictionary using the if key in temp syntax. If it is, we increment the value by 1 using the temp[key] += 1 syntax. If it’s not, we add a new key-value pair with a value of 1 using the temp[key] = 1 syntax.
We create a new list called res using a list comprehension. We iterate through each key-value pair in the temp dictionary using the temp.items() syntax, and create a new dictionary with keys “Gfg” and “freq” and the corresponding values from the temp dictionary. We enclose this dictionary in curly braces to create a dictionary object, and enclose this object in square brackets to create a list object.
We print the result using the print() function and str() function to convert the list to a string.

Python3




# initializing list
test_list = [{'Gfg': 1, 'best': 2},
             {'Gfg': 1, 'best': 2},
             {'Gfg': 2, 'good': 3},
             {'Gfg': 2, 'best': 2},
             {'Gfg': 2, 'good': 3}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Frequency Grouping Dictionary using a regular dictionary and a for loop
temp = {}
for sub in test_list:
    key = sub['Gfg']
    if key in temp:
        temp[key] += 1
    else:
        temp[key] = 1
 
res = [{"Gfg": key, "freq": val} for (key, val) in temp.items()]
 
# printing result
print("The frequency dictionary : " + str(res))


Output

The original list is : [{'Gfg': 1, 'best': 2}, {'Gfg': 1, 'best': 2}, {'Gfg': 2, 'good': 3}, {'Gfg': 2, 'best': 2}, {'Gfg': 2, 'good': 3}]
The frequency dictionary : [{'Gfg': 1, 'freq': 2}, {'Gfg': 2, 'freq': 3}]

Time complexity:  O(n), where n is the length of the test_list.

Auxiliary space:  O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads