Python – Values frequency across Dictionaries lists
Last Updated :
13 Apr, 2023
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 : test_list1 = [{“Gfg” : 6}], test_list2 = [{“a” : 6}, {“b” : 6}, {“d” : 6}}]
Output : {‘Gfg’: 3}
Explanation : 6 has 3 occurrence in 2nd list.
Method #1: Using dictionary comprehension + count() + list comprehension
The combination of above functionalities can be used to solve this problem. In this, we perform this task using 2 steps, in first we extract all values from second list, and then perform mapping with frequency with first dictionary using list comprehension and count().
Python3
test_list1 = [{ "Gfg" : 6 }, { "is" : 9 }, { "best" : 10 }]
test_list2 = [{ "a" : 6 }, { "b" : 10 }, { "c" : 9 }, { "d" : 6 }, { "e" : 9 }, { "f" : 9 }]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
temp = [val for sub in test_list2 for key, val in sub.items()]
res = {key : temp.count(val) for sub in test_list1 for key, val in sub.items()}
print ( "The frequency dictionary : " + str (res))
|
Output
The original list 1 : [{'Gfg': 6}, {'is': 9}, {'best': 10}]
The original list 2 : [{'a': 6}, {'b': 10}, {'c': 9}, {'d': 6}, {'e': 9}, {'f': 9}]
The frequency dictionary : {'Gfg': 2, 'is': 3, 'best': 1}
Time complexity: O(n^2), where n is the total number of elements in both the input lists.
Auxiliary space: O(n), where n is the total number of elements in both the input lists.
Method #2: Using dictionary comprehension + operator.countOf() + list comprehension
The combination of above functionalities can be used to solve this problem. In this, we perform this task using 2 steps, in first we extract all values from second list, and then perform mapping with frequency with first dictionary using list comprehension and operator.countOf().
Python3
import operator as op
test_list1 = [{ "Gfg" : 6 }, { "is" : 9 }, { "best" : 10 }]
test_list2 = [{ "a" : 6 }, { "b" : 10 }, { "c" : 9 }, { "d" : 6 }, { "e" : 9 }, { "f" : 9 }]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
temp = [val for sub in test_list2 for key, val in sub.items()]
res = {key : op.countOf(temp,val) for sub in test_list1 for key, val in sub.items()}
print ( "The frequency dictionary : " + str (res))
|
Output
The original list 1 : [{'Gfg': 6}, {'is': 9}, {'best': 10}]
The original list 2 : [{'a': 6}, {'b': 10}, {'c': 9}, {'d': 6}, {'e': 9}, {'f': 9}]
The frequency dictionary : {'Gfg': 2, 'is': 3, 'best': 1}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using a nested for loop
This program counts the frequency of values from the first list of dictionaries in the second list of dictionaries, and stores the results in a dictionary. The output is the frequency dictionary.
Python3
test_list1 = [{ "Gfg" : 6 }, { "is" : 9 }, { "best" : 10 }]
test_list2 = [{ "a" : 6 }, { "b" : 10 }, { "c" : 9 }, { "d" : 6 }, { "e" : 9 }, { "f" : 9 }]
print ( "The original list 1 : " + str (test_list1))
print ( "The original list 2 : " + str (test_list2))
res = {}
for dict1 in test_list1:
for key1, val1 in dict1.items():
count = 0
for dict2 in test_list2:
for key2, val2 in dict2.items():
if val2 = = val1:
count + = 1
res[key1] = count
print ( "The frequency dictionary : " + str (res))
|
Output
The original list 1 : [{'Gfg': 6}, {'is': 9}, {'best': 10}]
The original list 2 : [{'a': 6}, {'b': 10}, {'c': 9}, {'d': 6}, {'e': 9}, {'f': 9}]
The frequency dictionary : {'Gfg': 2, 'is': 3, 'best': 1}
Time complexity: O(n^3), where n is the length of the longer of the two input lists.
Auxiliary space: O(1) or constant.
Method #6: Using defaultdict
Another way to solve this problem is by using the defaultdict class from the collections module.
Step by step:
- Import the defaultdict class from the collections module.
- Initialize a defaultdict object with a default value of 0.
- Use a nested for loop to iterate through each dictionary in test_list1 and each key-value pair in the dictionary.
- Inside the nested for loop, use another nested for loop to iterate through each dictionary in test_list2 and each key-value pair in the dictionary.
- Check if the value of the current key-value pair in test_list1 is equal to the value of the current key-value pair in test_list2.
- If the values are equal, increment the corresponding key in the defaultdict object by 1.
- Convert the defaultdict object to a regular dictionary using the dict() constructor.
- Print the resulting dictionary.
Python3
from collections import defaultdict
test_list1 = [{ "Gfg" : 6 }, { "is" : 9 }, { "best" : 10 }]
test_list2 = [{ "a" : 6 }, { "b" : 10 }, { "c" : 9 }, { "d" : 6 }, { "e" : 9 }, { "f" : 9 }]
res = defaultdict( int )
for dict1 in test_list1:
for key1, val1 in dict1.items():
for dict2 in test_list2:
for key2, val2 in dict2.items():
if val2 = = val1:
res[key1] + = 1
res = dict (res)
print ( "The frequency dictionary : " + str (res))
|
Output
The frequency dictionary : {'Gfg': 2, 'is': 3, 'best': 1}
Time complexity: O(n^2), where n is the length of test_list1 multiplied by the length of test_list2.
Auxiliary space: O(k), where k is the number of unique values in test_list1.
Similar Reads
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 - 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.
3 min read
Python - Add custom values key in List of dictionaries
The task of adding custom values as keys in a list of dictionaries involves inserting a new key-value pair into each dictionary within the list. In Python, dictionaries are mutable, meaning the key-value pairs can be modified or added easily. When working with a list of dictionaries, the goal is to
5 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
Python - Common list elements and dictionary values
Given list and dictionary, extract common elements of List and Dictionary Values. Input : test_list = ["Gfg", "is", "Best", "For"], subs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", } Output : ['Gfg'] Explanation : "Gfg" is common in both list and dictionary value. Input : test_list = ["Gfg", "is",
3 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
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 - Cross mapping of Two dictionary value lists
GIven two dictionaries with list values, perform mapping of keys of first list with values of other list, by checking values-key linkage. Input : test_dict1 = {"Gfg" : [4, 10], "Best" : [8, 6], "is" : [9, 3]}, test_dict2 = {6 : [15, 9], 8 : [6, 3], 7 : [9, 8], 9 : [10, 11]} Output : {'Best': [6, 3,
6 min read
Python | Concatenate dictionary value lists
Sometimes, while working with dictionaries, we might have a problem in which we have lists as it's value and wish to have it cumulatively in single list by concatenation. This problem can occur in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Usi
5 min read
Python - Add Values to Dictionary of List
A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read