Python – Values frequencies of key
Sometimes, while working with Python dictionary lists, one can have a problem in which we require to find the frequency of all the values occurring in a specific key in dictionary list. This can have application across many domains including web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [{‘gfg’: [1]}, {‘good’: [5, 78, 10], ‘gfg’: [5, 6, 7, 8]}] Output : {8: 1, 1: 1, 5: 1, 6: 1, 7: 1} Input : test_list = [{‘gfg’: [1, 5, 6, 7]}] Output : {1: 1, 5: 1, 6: 1, 7: 1}
Method #1 : Using Counter() + list comprehension The combination of above functionalities is used to solve this problem. In this, we perform the task of finding frequency using Counter() and computation is done using list comprehension.
Python3
from collections import Counter
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ("The original list is : " + str (test_list))
freq_key = "gfg"
res = Counter([idx for val in test_list for idx in val[freq_key]])
print ("The frequency dictionary : " + str ( dict (res)))
|
Output :
The original list is : [{‘gfg’: [1, 5, 6, 7], ‘is’: [9, 6, 2, 10]}, {‘gfg’: [5, 6, 7, 8], ‘good’: [5, 7, 10]}, {‘gfg’: [7, 5], ‘best’: [5, 7]}] The frequency dictionary : {8: 1, 1: 1, 5: 3, 6: 2, 7: 3}
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using chain.from_iterables() + Counter() The combination of above methods can be used to solve this problem. In this, we perform the task of iteration and binding using chain.from_iterables().
Python3
from collections import Counter
import itertools
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ("The original list is : " + str (test_list))
freq_key = "gfg"
res = Counter(itertools.chain.from_iterable([sub[freq_key] for sub in test_list]))
print ("The frequency dictionary : " + str ( dict (res)))
|
Output :
The original list is : [{‘gfg’: [1, 5, 6, 7], ‘is’: [9, 6, 2, 10]}, {‘gfg’: [5, 6, 7, 8], ‘good’: [5, 7, 10]}, {‘gfg’: [7, 5], ‘best’: [5, 7]}] The frequency dictionary : {8: 1, 1: 1, 5: 3, 6: 2, 7: 3}
Time Complexity: O(n*n) where n is the number of elements in the dictionary. The chain.from_iterables() + Counter() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Method #3 : Using extend(),list(),set(),count() methods
Approach
- Extracting all values of a particular key in list of dictionaries using for loop and extend() method
- Create a new list with all unique values of above values list
- Now create a dictionary with unique value as key and count of this value in the values list using count() method
- Display dictionary
Python3
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ( "The original list is : " + str (test_list))
freq_key = "gfg"
res = dict ()
x = []
for i in test_list:
x.extend(i[freq_key])
y = list ( set (x))
for i in y:
res[i] = x.count(i)
print ( "The frequency dictionary : " + str (res))
|
Output
The original list is : [{'gfg': [1, 5, 6, 7], 'is': [9, 6, 2, 10]}, {'gfg': [5, 6, 7, 8], 'good': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The frequency dictionary : {1: 1, 5: 3, 6: 2, 7: 3, 8: 1}
Time Complexity : O(N)
Auxiliary Space : O(N)
Method #4:Using Numpy
Algorithm
- Initialize the list of dictionaries.
- Print the original list.
- Initialize the frequency key for which we want to find the values frequency.
- Initialize an empty dictionary for storing the frequency of values of the frequency key.
- For each dictionary in the list, extend the values corresponding to the frequency key to a list x.
- Get the unique values of list x using set().
- For each unique value in the list of unique values, count the frequency of the value in the list x using count() function and store the frequency in the resultant dictionary.
- Print the frequency dictionary.
Python3
import numpy as np
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ( "The original list is:" , test_list)
freq_key = "gfg"
res = {}
flat_arr = np.concatenate([d[freq_key] for d in test_list])
unique, counts = np.unique(flat_arr, return_counts = True )
for i in range ( len (unique)):
res[unique[i]] = counts[i]
print ( "The frequency dictionary:" , res)
|
Output
The original list is: [{'gfg': [1, 5, 6, 7], 'is': [9, 6, 2, 10]}, {'gfg': [5, 6, 7, 8], 'good': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The frequency dictionary: {1: 1, 5: 3, 6: 2, 7: 3, 8: 1}
The time complexity of the given code is O(N*M), where N is the number of dictionaries in the list and M is the maximum length of the list associated with the given frequency key.
The space complexity of the given code is also O(N*M), since we are creating a list to store all the values associated with the given frequency key, and then creating a dictionary with keys as unique values and their count as values.
Method 5 : Using a loop
step-by-step approach:
- Initialize an empty dictionary called “freq_dict” to store the frequency count of each value in the “gfg” key.
- Loop through each dictionary in the list “test_list”.
- Access the values of the “gfg” key in the current dictionary using the syntax “current_dict[‘gfg’]”.
- Loop through each value in the “gfg” key using a for loop.
- For each value in the “gfg” key, check if it is already a key in the “freq_dict” dictionary. If it is, increment its value by 1. If it is not, add it as a new key with value 1.
- After iterating through all the dictionaries in “test_list”, “freq_dict” will contain the frequency count of each value in the “gfg” key.
- Print the “freq_dict” dictionary.
Python3
test_list = [{ 'gfg' : [ 1 , 5 , 6 , 7 ], 'is' : [ 9 , 6 , 2 , 10 ]},
{ 'gfg' : [ 5 , 6 , 7 , 8 ], 'good' : [ 5 , 7 , 10 ]},
{ 'gfg' : [ 7 , 5 ], 'best' : [ 5 , 7 ]}]
print ( "The original list is:" , test_list)
freq_key = "gfg"
freq_dict = {}
for d in test_list:
for val in d[freq_key]:
if val in freq_dict:
freq_dict[val] + = 1
else :
freq_dict[val] = 1
print ( "The frequency dictionary:" , freq_dict)
|
Output
The original list is: [{'gfg': [1, 5, 6, 7], 'is': [9, 6, 2, 10]}, {'gfg': [5, 6, 7, 8], 'good': [5, 7, 10]}, {'gfg': [7, 5], 'best': [5, 7]}]
The frequency dictionary: {1: 1, 5: 3, 6: 2, 7: 3, 8: 1}
Time complexity: O(N*M), where N is the number of dictionaries in “test_list” and M is the average length of the “gfg” key in each dictionary.
Auxiliary space: O(K), where K is the number of unique values in the “gfg” key across all dictionaries in “test_list”.
Last Updated :
05 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...