Open In App

Python – Associated Values Frequencies in Dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with dictionaries, we can have problem in which we need to compute the values associated to each value in dictionary in records list. This kind of problem is peculiar, but can have application in development domains. Lets discuss certain way in which this task can be performed. 

Counting  Associated Values Frequencies in Dictionary

Let us see a few methods by which we can calculate associated values frequencies in a Python Dictionary.

Using nested defaultdict() + Counter() 

The combination of defaultdict() and counter() functions can be used to solve this problem. In this, we use nested defaultdict to keep track of elements, and counting of elements is done by Counter(). 

Python3




# Python3 code to demonstrate working of
# Associated Values Frequencies in Dictionary
# Using defaultdict() + Counter()
from collections import defaultdict, Counter
 
# initializing list
test_list = [{'gfg' : 1, 'is' : 3, 'best' : 4},
             {'gfg' : 3, 'is' : 2, 'best' : 4},
             {'gfg' : 3, 'is' : 5, 'best' : 2},
             {'gfg' : 5, 'is' : 2, 'best' : 1},
             {'gfg' : 2, 'is' : 4, 'best' : 3},
             {'gfg' : 1, 'is' : 3, 'best' : 5},
             {'gfg' : 1, 'is' : 3, 'best' : 2}]
 
# Associated Values Frequencies in Dictionary
# Using defaultdict() + Counter()
res = defaultdict(Counter)
for sub in test_list:
    for key, val in sub.items():
        res[key][val] += 1
 
# printing result
print("The list after Frequencies : " + str(dict(res)))


Output:

The list after Frequencies : {'gfg': Counter({1: 3, 3: 2, 5: 1, 2: 1}), 
'is': Counter({3: 3, 2: 2, 5: 1, 4: 1}), 'best': Counter({4: 2, 2: 2, 1: 1, 3: 1, 5: 1})}

Time complexity: O(n*n), where n is the number of values in the dictionary.
Auxiliary Space: O(1), constant extra space is required

Using loops and a dictionary

In this approach, we will use nested loops to iterate through the original list and store the frequencies in a dictionary.

Python3




from collections import Counter
 
original_list = [{'gfg': 1, 'is': 3, 'best': 4},
                 {'gfg': 3, 'is': 2, 'best': 4},
                 {'gfg': 3, 'is': 5, 'best': 2},
                 {'gfg': 5, 'is': 2, 'best': 1},
                 {'gfg': 2, 'is': 4, 'best': 3},
                 {'gfg': 1, 'is': 3, 'best': 5},
                 {'gfg': 1, 'is': 3, 'best': 2}]
 
frequencies = {}
 
for item in original_list:
    for key, value in item.items():
        if key not in frequencies:
            frequencies[key] = Counter()
        frequencies[key][value] += 1
 
print(frequencies)


Output:

{'gfg': Counter({1: 3, 3: 2, 5: 1, 2: 1}), 
'is': Counter({3: 3, 2: 2, 5: 1, 4: 1}), 'best': Counter({4: 2, 2: 2, 1: 1, 3: 1, 5: 1})}

Time complexity: O(n^2), where n is the size of the list
Space complexity: O(n)

Using a dictionary comprehension 

This algorithm uses a dictionary comprehension to iterate over the keys in the first dictionary of test_list and create a new dictionary freq_dict where each key maps to a Counter object containing the frequency counts of the corresponding value in each dictionary in test_list. The Counter function is used to count the frequencies.

ALGORITHM:

1.Initialize an empty dictionary to store the frequencies of values for each key.
2.Use dictionary comprehension with the Counter() function to iterate over each key in the first dictionary of test_list, and count the frequency of values for that key across all the dictionaries in test_list.
3.Return the resulting dictionary of dictionaries containing the frequencies of values for each key.

Python3




from collections import Counter
 
test_list = [{'gfg' : 1, 'is' : 3, 'best' : 4},
             {'gfg' : 3, 'is' : 2, 'best' : 4},
             {'gfg' : 3, 'is' : 5, 'best' : 2},
             {'gfg' : 5, 'is' : 2, 'best' : 1},
             {'gfg' : 2, 'is' : 4, 'best' : 3},
             {'gfg' : 1, 'is' : 3, 'best' : 5},
             {'gfg' : 1, 'is' : 3, 'best' : 2}]
 
freq_dict = {k: Counter(d[k] for d in test_list) for k in test_list[0]}
print(freq_dict)


Output:

{'gfg': Counter({1: 3, 3: 2, 5: 1, 2: 1}), 
'is': Counter({3: 3, 2: 2, 5: 1, 4: 1}), 'best': Counter({4: 2, 2: 2, 1: 1, 3: 1, 5: 1})}

Time complexity: O(nm), where n is the number of dictionaries in test_list and m is the number of keys in each dictionary.

Auxiliary Space: O(m), where m is the number of keys in each dictionary.

Using the Pandas library 

Approach:

  1. Import the Python Pandas library using the import pandas as pd statement.
  2. Define the test_list variable as a list of dictionaries, where each dictionary represents a row in a table.
  3. Convert the list of dictionaries to a pandas DataFrame using the pd.DataFrame() constructor.
  4. Define an empty dictionary freq_dict to store the frequency of each value for each key in the DataFrame.
  5. Iterate over each column (key) in the DataFrame using a for loop.
  6. For each column, use the value_counts() method to count the frequency of each value and store the resulting Series as a dictionary using the to_dict() method.
  7. Store the frequency dictionary for each key in the freq_dict dictionary.
  8. Print the freq_dict dictionary.

Python3




import pandas as pd
 
test_list = [{'gfg' : 1, 'is' : 3, 'best' : 4},           
             {'gfg' : 3, 'is' : 2, 'best' : 4},           
             {'gfg' : 3, 'is' : 5, 'best' : 2},           
             {'gfg' : 5, 'is' : 2, 'best' : 1},           
             {'gfg' : 2, 'is' : 4, 'best' : 3},           
             {'gfg' : 1, 'is' : 3, 'best' : 5},           
             {'gfg' : 1, 'is' : 3, 'best' : 2}]
 
df = pd.DataFrame(test_list)
freq_dict = {}
for k in df.columns:
    freq_dict[k] = df[k].value_counts().to_dict()
 
print(freq_dict)


Output:

{'gfg': {1: 3, 3: 2, 5: 1, 2: 1}, 'is': {3: 3, 2: 2, 5: 1, 4: 1}, 'best': {4: 2, 2: 2, 1: 1, 3: 1, 5: 1}}

Time complexity: O(NKV), where N is the number of dictionaries in the list, K is the average number of keys in a dictionary, and V is the average number of unique values for a key. 

Auxiliary space: O(NKV), since we are creating a DataFrame and several dictionaries to store the frequencies.



Last Updated : 01 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads