 GeeksforGeeks App
Open App Browser
Continue

# 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:

## Python3

 `# 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:

## Python3

 `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)]```

#### 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.

## Python3

 `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:

• Import the defaultdict from the collections module.
Define a function freq_dict_2 that takes a list of dictionaries as input.
• Initialize a defaultdict of integers, called freq_dict.
• Loop through each dictionary in the input list.
• Convert the dictionary to a frozenset, so it can be used as a key in the defaultdict.
• Increment the frequency of the frozenset key in the defaultdict by 1.
• Create a list of tuples using a list comprehension, where each tuple contains the dictionary (converted from the frozenset), and its frequency.
• Sort the list of tuples by frequency in descending order.
• Return the sorted list of tuples.

## Python3

 `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.

My Personal Notes arrow_drop_up