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
from collections import defaultdict
test_dict = { 1 : [ 'gfg' , 'best' , 'geeks' ],
2 : [ 'gfg' , 'CS' ],
3 : [ 'best' , 'for' , 'CS' ],
4 : [ 'test' , 'ide' , 'success' ],
5 : [ 'gfg' , 'is' , 'best' ]}
print ( "The original dictionary : " + str (test_dict))
res = defaultdict( int )
for key, val in test_dict.items():
for sub in val:
res[sub] + = 1
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
from collections import Counter
from itertools import chain
test_dict = { 1 : [ 'gfg' , 'best' , 'geeks' ],
2 : [ 'gfg' , 'CS' ],
3 : [ 'best' , 'for' , 'CS' ],
4 : [ 'test' , 'ide' , 'success' ],
5 : [ 'gfg' , 'is' , 'best' ]}
print ( "The original dictionary : " + str (test_dict))
res = Counter(chain.from_iterable(test_dict.values()))
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)))
|
Output
Values 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
test_dict = { 1 : [ 'gfg' , 'best' , 'geeks' ],
2 : [ 'gfg' , 'CS' ],
3 : [ 'best' , 'for' , 'CS' ],
4 : [ 'test' , 'ide' , 'success' ],
5 : [ 'gfg' , 'is' , 'best' ]}
print ( "The original dictionary : " + str (test_dict))
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)
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 : {'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' ]}
print ( "The original dictionary : " + str (test_dict))
list_values = np.hstack( list (test_dict.values()))
unique, counts = np.unique(list_values, return_counts = True )
frequency_dict = dict ( zip (unique, counts))
print ( "Values Frequency : " + str (frequency_dict))
|
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
Inverse Dictionary Values List - Python
We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]} Using defau
2 min read
Get List of Values From Dictionary - Python
We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3]. Using dict.values()We can use dict.values() along with the list() function to get the list. Here,
2 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 - Value length dictionary
Sometimes, while working with a Python dictionary, we can have problems in which we need to map the value of the dictionary to its length. This kind of application can come in many domains including web development and day-day programming. Let us discuss certain ways in which this task can be perfor
4 min read
Python - Value Dictionary from Record List
Sometimes, while working with Python Records lists, we can have problems in which, we need to reform the dictionary taking just values of the binary dictionary. This can have applications in many domains which work with data. Let us discuss certain ways in which this task can be performed. Method #1
6 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 Print Dictionary Keys and Values
When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values. Example: Using print() Method [GFGTABS] Python my_dict = {'a': 1, 'b'
2 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
Get Python Dictionary Values as List - Python
We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5] Using itertools.chain()itertools.chain() function efficientl
2 min read