Python – Find occurrences for each value of a particular key
Last Updated :
30 Apr, 2023
Given a List of dictionaries, for a particular key, find the number of occurrences for each value of that key.
Input : test_list = [{‘gfg’ : 3, ‘best’ : 4}, {‘gfg’ : 3, ‘best’ : 5},
{‘gfg’ : 4, ‘best’ : 4}, {‘gfg’ : 7, ‘best’ : 4} ], K = ‘gfg’
Output : [{3: 2}, {4: 1}, {7: 1}]
Explanation : gfg has 2 occurrences of 3 as values.
Input : test_list = [{‘gfg’ : 3, ‘best’ : 4}, {‘gfg’ : 3, ‘best’ : 5},
{‘gfg’ : 4, ‘best’ : 4}, {‘gfg’ : 7, ‘best’ : 4} ], K = ‘best’
Output : [{4: 3}, {5: 1}]
Explanation : best has 3 occurrences of 4 as values.
Method #1 : Using groupby() + dictionary comprehension
In this, we perform grouping of key’s values using groupby() and values frequency is assembled and extracted using dictionary comprehension and len().
Python3
from itertools import groupby
test_list = [{ 'gfg' : 3 , 'best' : 4 }, { 'gfg' : 3 , 'best' : 5 },
{ 'gfg' : 4 , 'best' : 4 }, { 'gfg' : 7 , 'best' : 4 } ]
print ( "The original list is : " + str (test_list))
K = 'gfg'
res = [{key: len ( list (val))} for key, val in groupby(test_list, lambda sub: sub[K])]
print ( "The Values Frequency : " + str (res))
|
Output:
The original list is : [{‘gfg’: 3, ‘best’: 4}, {‘gfg’: 3, ‘best’: 5}, {‘gfg’: 4, ‘best’: 4}, {‘gfg’: 7, ‘best’: 4}] The Values Frequency : [{3: 2}, {4: 1}, {7: 1}]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. groupby() + dictionary comprehension performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using Counter()
In this, the task of performing frequency check is done using Counter(). Returns result in single dictionary.
Python3
from collections import Counter
test_list = [{ 'gfg' : 3 , 'best' : 4 }, { 'gfg' : 3 , 'best' : 5 },
{ 'gfg' : 4 , 'best' : 4 }, { 'gfg' : 7 , 'best' : 4 } ]
print ( "The original list is : " + str (test_list))
K = 'gfg'
res = dict (Counter(sub[K] for sub in test_list))
print ( "The Values Frequency : " + str (res))
|
Output:
The original list is : [{‘gfg’: 3, ‘best’: 4}, {‘gfg’: 3, ‘best’: 5}, {‘gfg’: 4, ‘best’: 4}, {‘gfg’: 7, ‘best’: 4}] The Values Frequency : [{3: 2}, {4: 1}, {7: 1}]
Method #3: Using keys(),list(),set() and count() methods
Python3
test_list = [{ 'gfg' : 3 , 'best' : 4 }, { 'gfg' : 3 , 'best' : 5 },
{ 'gfg' : 4 , 'best' : 4 }, { 'gfg' : 7 , 'best' : 4 } ]
print ( "The original list is : " + str (test_list))
K = 'gfg'
x = []
for i in test_list:
if K in i.keys():
x.append(i[K])
p = list ( set (x))
nl = []
for i in p:
d = {}
d[i] = x.count(i)
nl.append(d)
print ( "The Values Frequency : " + str (nl))
|
Output
The original list is : [{'gfg': 3, 'best': 4}, {'gfg': 3, 'best': 5}, {'gfg': 4, 'best': 4}, {'gfg': 7, 'best': 4}]
The Values Frequency : [{3: 2}, {4: 1}, {7: 1}]
Method#4:Using defaultdict() and loop
Algorithm:
- Create an empty dictionary and set the default value to 0.
- Iterate over the list of dictionaries.
- For each dictionary, get the value of key K.
- Use this value to update the count in the dictionary created in step 1.
- Return the resulting dictionary.
Python3
from collections import defaultdict
test_list = [{ 'gfg' : 3 , 'best' : 4 }, { 'gfg' : 3 , 'best' : 5 },
{ 'gfg' : 4 , 'best' : 4 }, { 'gfg' : 7 , 'best' : 4 } ]
K = 'gfg'
freq_dict = defaultdict( int )
for item in test_list:
freq_dict[item[K]] + = 1
res = [{key: val} for key, val in freq_dict.items()]
print ( "The Values Frequency : " + str (res))
|
Output
The Values Frequency : [{3: 2}, {4: 1}, {7: 1}]
Time Complexity: O(n), where n is the number of dictionaries in the list. This is because we need to iterate over each dictionary once.
Space Complexity: O(k), where k is the number of unique values for key K. This is because we are storing the count for each unique value in the resulting dictionary.
Method #5: Using pandas
Step-by-step approach:
Import pandas library.
Convert the list of dictionaries to a pandas DataFrame using the pd.DataFrame() function.
Use the value_counts() method on the ‘gfg’ column of the DataFrame to get the frequency of each unique value.
Convert the resulting pandas Series object to a dictionary using the to_dict() method.
Format the dictionary as a list of dictionaries using a list comprehension.
Print the resulting list of dictionaries.
Python3
import pandas as pd
test_list = [{ 'gfg' : 3 , 'best' : 4 }, { 'gfg' : 3 , 'best' : 5 },
{ 'gfg' : 4 , 'best' : 4 }, { 'gfg' : 7 , 'best' : 4 } ]
K = 'gfg'
df = pd.DataFrame(test_list)
freq_dict = df[K].value_counts().to_dict()
res = [{key: val} for key, val in freq_dict.items()]
print ( "The Values Frequency : " + str (res))
|
OUTPUT : The Values Frequency : [{3: 2}, {4: 1}, {7: 1}]
Time complexity: O(n log n), where n is the number of items in the input list.
Auxiliary space: O(n), where n is the number of items in the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...