Open In App

Python Program to calculate Dictionaries frequencies

Given a list of dictionaries, the task here is to write a python program to extract dictionary frequencies of each dictionary.

Input : test_list = [{‘gfg’ : 1, ‘is’ : 4, ‘best’ : 9},



             {‘gfg’ : 6, ‘is’ : 3, ‘best’ : 8},

             {‘gfg’ : 1, ‘is’ : 4, ‘best’ : 9},



             {‘gfg’ : 1, ‘is’ : 1, ‘best’ : 9},

             {‘gfg’ : 6, ‘is’ : 3, ‘best’ : 8}]

Output : [({‘gfg’: 1, ‘is’: 4, ‘best’: 9}, 2), ({‘gfg’: 6, ‘is’: 3, ‘best’: 8}, 2), ({‘gfg’: 1, ‘is’: 1, ‘best’: 9}, 1)]
Explanation : Dictionaries with their frequency appended as result in list.

Input : test_list = [{‘gfg’ : 1, ‘is’ : 4, ‘best’ : 9},

             {‘gfg’ : 6, ‘is’ : 3, ‘best’ : 8},

             {‘gfg’ : 1, ‘is’ : 4, ‘best’ : 9},

             {‘gfg’ : 1, ‘is’ : 1, ‘best’ : 9}]

Output : [({‘gfg’: 1, ‘is’: 4, ‘best’: 9}, 2), ({‘gfg’: 6, ‘is’: 3, ‘best’: 8}, 1), ({‘gfg’: 1, ‘is’: 1, ‘best’: 9}, 1)]
Explanation : Dictionaries with their frequency appended as result in list.

Method 1 : Using index() and loop

In this, each dictionary is iterated and index() is used to get the index of dictionary mapped with its increasing frequencies, and increment counter in case of repeated dictionary.

Example:




# initializing list
test_list = [{'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8},
             {'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 1, 'is': 1, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8}]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for sub in test_list:
 
    flag = 0
    for ele in res:
 
        # checking for presence and incrementing frequency
        if sub == ele[0]:
            res[res.index(ele)] = (sub, ele[1] + 1)
            flag = 1
 
    if not flag:
        res.append((sub, 1))
 
# printing result
print("Dictionaries frequencies : " + str(res))

Output
The original list is : [{'gfg': 1, 'is': 4, 'best': 9}, {'gfg': 6, 'is': 3, 'best': 8}, {'gfg': 1, 'is': 4, 'best': 9}, {'gfg': 1, 'is': 1, 'best': 9}, {'gfg': 6, 'is': 3, 'best': 8}]
Dictionaries frequencies : [({'gfg': 1, 'is': 4, 'best': 9}, 2), ({'gfg': 6, 'is': 3, 'best': 8}, 2), ({'gfg': 1, 'is': 1, 'best': 9}, 1)]

Method 2 : Using Counter() and sorted()

In this, dictionary elements are converted to tuple pairs and then Counter is used to get frequency of each. At last step, each dictionary is reconverted to its original form.

Example:




from collections import Counter
 
# initializing list
test_list = [{'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8},
             {'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 1, 'is': 1, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8}]
 
# printing original list
print("The original list is : " + str(test_list))
 
# getting frequencies
temp = Counter(tuple(sorted(sub.items())) for sub in test_list)
 
# converting back to Dictionaries
res = [(dict([tuple(ele) for ele in sub]), temp[sub]) for sub in temp]
 
# printing result
print("Dictionaries frequencies : " + str(res))

Output
The original list is : [{'gfg': 1, 'is': 4, 'best': 9}, {'gfg': 6, 'is': 3, 'best': 8}, {'gfg': 1, 'is': 4, 'best': 9}, {'gfg': 1, 'is': 1, 'best': 9}, {'gfg': 6, 'is': 3, 'best': 8}]
Dictionaries frequencies : [({'best': 9, 'gfg': 1, 'is': 4}, 2), ({'best': 8, 'gfg': 6, 'is': 3}, 2), ({'best': 9, 'gfg': 1, 'is': 1}, 1)]

Method 3 : Using lambda+ list of dictionary

Approach

we will iterate through the given list of dictionaries and use a dictionary to keep track of the frequency of each dictionary. Finally, we will return the list of dictionaries along with their frequency in descending order of frequency.

Algorithm

1. Create an empty dictionary freq_dict to store the frequency of each dictionary.
2. Iterate through the given list of dictionaries.
3. For each dictionary, check if it is present in freq_dict. If it is not present, add it with a frequency of 1. If it is already present, increment its frequency.
4. Create a list of tuples from freq_dict, where each tuple contains the dictionary and its frequency.
5. Sort the list of tuples in descending order of frequency.
6. Return the sorted list of tuples.




def freq_dict_1(test_list):
    freq_dict = {}
    for d in test_list:
        if str(d) not in freq_dict:
            freq_dict[str(d)] = 1
        else:
            freq_dict[str(d)] += 1
    freq_list = [(eval(k), v) for k, v in freq_dict.items()]
    freq_list.sort(key=lambda x: x[1], reverse=True)
    return freq_list
test_list = [{'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8},
             {'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 1, 'is': 1, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8}]
print(freq_dict_1(test_list))

Output
[({'gfg': 1, 'is': 4, 'best': 9}, 2), ({'gfg': 6, 'is': 3, 'best': 8}, 2), ({'gfg': 1, 'is': 1, 'best': 9}, 1)]

Time Complexity: O(n log n), where n is the length of the given list of dictionaries. The sort() function has a time complexity of O(n log n).
Auxiliary Space: O(n), where n is the length of the given list of dictionaries. The freq_dict dictionary will have at most n entries.

Method 4: Using defaultdict and list comprehension

The defaultdict is a subclass of the dict class that provides a default value for a nonexistent key. Here, we can use it to initialize the frequency of each dictionary to zero, and then increment it as we encounter it in the list.

Step-by-step approach:




from collections import defaultdict
 
def freq_dict_2(test_list):
    freq_dict = defaultdict(int)
    for d in test_list:
        freq_dict[frozenset(d.items())] += 1
    freq_list = [(dict(k), v) for k, v in freq_dict.items()]
    freq_list.sort(key=lambda x: x[1], reverse=True)
    return freq_list
 
test_list = [{'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8},
             {'gfg': 1, 'is': 4, 'best': 9},
             {'gfg': 1, 'is': 1, 'best': 9},
             {'gfg': 6, 'is': 3, 'best': 8}]
print(freq_dict_2(test_list))

Output
[({'is': 4, 'gfg': 1, 'best': 9}, 2), ({'gfg': 6, 'is': 3, 'best': 8}, 2), ({'is': 1, 'gfg': 1, 'best': 9}, 1)]

Time Complexity: O(nlogn), where n is the length of the input list. 
Auxiliary Space: O(n), where n is the length of the input list.


Article Tags :