Python - Dictionary List Values Frequency
Last Updated :
25 Apr, 2023
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the task of computing frequency of all the values in dictionary values lists. This is quite common problem and can have use cases in many domains. Let's discuss certain ways in which this task can be performed.
Input : test_dict = {1: ['gfg', 'CS', 'cool'], 2: ['gfg', 'CS']}
Output : {'gfg': 2, 'CS': 2, 'cool': 1}
Input : test_dict = {1 : ['gfg', 'CS']}
Output : {'gfg': 1, 'CS': 1}
Method #1 : Using defaultdict() + loop
The combination of above functions can be used to solve this problem. In this, we use defaultdict() to initialize the counter for each value and brute force is used to increment the counter in dictionary lists.
Python3
# Python3 code to demonstrate working of
# Dictionary List Values Frequency
# Using loop + defaultdict()
from collections import defaultdict
# initializing dictionary
test_dict = {1 : ['gfg', 'best', 'geeks'],
2 : ['gfg', 'CS'],
3 : ['best', 'for', 'CS'],
4 : ['test', 'ide', 'success'],
5 : ['gfg', 'is', 'best']}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# Dictionary List Values Frequency
# Using loop + defaultdict()
res = defaultdict(int)
for key, val in test_dict.items():
for sub in val:
res[sub] += 1
# printing result
print("Values Frequency : " + str(dict(res)))
Output :
The original dictionary : {1: ['gfg', 'best', 'geeks'], 2: ['gfg', 'CS'], 3: ['best', 'for', 'CS'], 4: ['test', 'ide', 'success'], 5: ['gfg', 'is', 'best']}
Values Frequency : {'gfg': 3, 'best': 3, 'geeks': 1, 'CS': 2, 'for': 1, 'test': 1, 'ide': 1, 'success': 1, 'is': 1}
Time Complexity: O(nm), where n is the total number of keys in the input "test_dict" and m is the total number of elements in all the lists in the dictionary values.
Space Complexity: O(k), where k is the total number of unique elements in all the lists in the dictionary values.
Method #2 : Using chain.from_iterables() + Counter()
The combination of above functions can be used to solve this problem. In this, we perform task of Counting using Counter() and the flattening/extraction of values is done using from_iterables().
Python3
# Python3 code to demonstrate working of
# Dictionary List Values Frequency
# Using chain.from_iterables() + Counter()
from collections import Counter
from itertools import chain
# initializing dictionary
test_dict = {1 : ['gfg', 'best', 'geeks'],
2 : ['gfg', 'CS'],
3 : ['best', 'for', 'CS'],
4 : ['test', 'ide', 'success'],
5 : ['gfg', 'is', 'best']}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# Dictionary List Values Frequency
# Using chain.from_iterables() + Counter()
res = Counter(chain.from_iterable(test_dict.values()))
# printing result
print("Values Frequency : " + str(dict(res)))
Output :
The original dictionary : {1: ['gfg', 'best', 'geeks'], 2: ['gfg', 'CS'], 3: ['best', 'for', 'CS'], 4: ['test', 'ide', 'success'], 5: ['gfg', 'is', 'best']}
Values Frequency : {'gfg': 3, 'best': 3, 'geeks': 1, 'CS': 2, 'for': 1, 'test': 1, 'ide': 1, 'success': 1, 'is': 1}
Method #3: Using Counter() + list comprehension
- Import Counter from collections
- Define the original dictionary test_dict
- Use a list comprehension to flatten the values of test_dict into a list of all values
- Use Counter to count the frequency of each value in the flattened list
- Print the resulting dictionary of value frequencies
Python3
from collections import Counter
test_dict = {1 : ['gfg', 'best', 'geeks'],
2 : ['gfg', 'CS'],
3 : ['best', 'for', 'CS'],
4 : ['test', 'ide', 'success'],
5 : ['gfg', 'is', 'best']}
res = Counter([val for sublist in test_dict.values() for val in sublist])
print("Values Frequency : " + str(dict(res)))
OutputValues Frequency : {'gfg': 3, 'best': 3, 'geeks': 1, 'CS': 2, 'for': 1, 'test': 1, 'ide': 1, 'success': 1, 'is': 1}
Time complexity: O(n), where n is the total number of values in test_dict.
Auxiliary space: O(n), for the list of all values.
Method #4 : Using values(),list(),set(),extend(),count() methods
Approach
- Store the values of dictionary test_dict in x(using list(),values() method) which is a nested list
- Convert nested list to list y using extend and for loop
- Store unique values of y in z using list(),set() methods, create an empty dictionary res
- Initiate a for loop over list z and assign values of z as keys and the count of those values in y as values to dictionary res
- Display res
Python3
# Python3 code to demonstrate working of
# Dictionary List Values Frequency
# initializing dictionary
test_dict = {1 : ['gfg', 'best', 'geeks'],
2 : ['gfg', 'CS'],
3 : ['best', 'for', 'CS'],
4 : ['test', 'ide', 'success'],
5 : ['gfg', 'is', 'best']}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# Dictionary List Values Frequency
x=list(test_dict.values())
y=[]
for i in x:
y.extend(i)
z=list(set(y))
res=dict()
for i in z:
res[i]=y.count(i)
# printing result
print("Values Frequency : " + str(dict(res)))
OutputThe original dictionary : {1: ['gfg', 'best', 'geeks'], 2: ['gfg', 'CS'], 3: ['best', 'for', 'CS'], 4: ['test', 'ide', 'success'], 5: ['gfg', 'is', 'best']}
Values Frequency : {'test': 1, 'gfg': 3, 'ide': 1, 'best': 3, 'CS': 2, 'is': 1, 'success': 1, 'for': 1, 'geeks': 1}
Time Complexity : O(N) N -length of extended list(y)
Auxiliary Space : O(N) N - length of output dictionary
Method #5 : Using numpy:
Algorithm :
- Create a dictionary test_dict.
- Use numpy.hstack() to flatten all the values in the dictionary into a single list named list_values.
- Use numpy.unique() to count the frequency of each item in the list list_values and return the unique values and their counts in two separate lists named unique and counts.
- Use dict(zip()) to create a new dictionary frequency_dict where unique and counts are zipped together as key-value pairs.
- Print the frequency dictionary
Python3
import numpy as np
test_dict = {1 : ['gfg', 'best', 'geeks'],
2 : ['gfg', 'CS'],
3 : ['best', 'for', 'CS'],
4 : ['test', 'ide', 'success'],
5 : ['gfg', 'is', 'best']}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# Using numpy.hstack() to flatten dictionary values into a single list
list_values = np.hstack(list(test_dict.values()))
# Using numpy.unique() to count frequency of each item in the list
unique, counts = np.unique(list_values, return_counts=True)
# Creating the frequency dictionary
frequency_dict = dict(zip(unique, counts))
# printing result
print("Values Frequency : " + str(frequency_dict))
#This code is contrinuted by Pushpa.
Output:
The original dictionary : {1: ['gfg', 'best', 'geeks'], 2: ['gfg', 'CS'], 3: ['best', 'for', 'CS'], 4: ['test', 'ide', 'success'], 5: ['gfg', 'is', 'best']}
Values Frequency : {'CS': 2, 'best': 3, 'for': 1, 'geeks': 1, 'gfg': 3, 'ide': 1, 'is': 1, 'success': 1, 'test': 1}
Time Complexity:
Flattening the dictionary values into a single list takes O(n) time, where n is the total number of values in all lists combined.
Counting the frequency of each item in the list takes O(k log k) time, where k is the number of unique items in the list.
Zipping the unique values and their counts as key-value pairs into a new dictionary takes O(k) time.
Therefore, the overall time complexity of the code is O(n + k log k).
Space Complexity:
The space used by the original dictionary test_dict is O(n).
The space used by the list_values list is also O(n).
The space used by the unique and counts lists is O(k).
The space used by the frequency_dict dictionary is also O(k).
Therefore, the overall space complexity of the code is O(n + k).
Similar Reads
Python - Values frequency across Dictionaries lists Given two list of dictionaries, compute frequency corresponding to each value in dictionary 1 to second. Input : test_list1 = [{"Gfg" : 6}, {"best" : 10}], test_list2 = [{"a" : 6}, {"b" : 10}, {"d" : 6}}] Output : {'Gfg': 2, 'best': 1} Explanation : 6 has 2 occurrence in 2nd list, 10 has 1. Input :
6 min read
Python - Convert Frequency dictionary to list When we convert a frequency dictionary to a list, we are transforming the dictionary into a list of key-value or just the keys/values, depending on needs. We can convert a frequency dictionary to a list using methods such as list comprehension, loops, extend() method and itertools.chain() function.F
3 min read
Python - Associated Values Frequencies in Dictionary 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 perform
5 min read
Python - Dictionary Values Mean Given a dictionary, find the mean of all the values present. Input : test_dict = {"Gfg" : 4, "is" : 4, "Best" : 4, "for" : 4, "Geeks" : 4} Output : 4.0 Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean. Input : test_dict = {"Gfg" : 5, "is" : 10, "Best" : 15} Output : 10.0 Explanation : Mean of
4 min read
Python - Values Frequency Index List Sometimes, while working with Python tuples, we can have a problem in which we need to extract the frequency of each value in tuple. This has been solved earlier. We can have a modification in which we need to create list in which index represents the key and value of it represents the frequency of
4 min read
Python - Frequency Grouping Dictionary Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the grouping of dictionary data, in a way in which we need to group all the similar dictionaries key with its frequency. This kind of problem has its application in web development domain. Let's disc
4 min read