Given a dictionary with values as a list, extract all the possible combinations, both cross keys and with values.
Input : test_dict = {“Gfg” : [4, 5], “is” : [1, 2], “Best” : [9, 4]}
Output : {0: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 9]], 1: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 4]], 2: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 9]], 3: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 4]], 4: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 9]], 5: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 4]], 6: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 9]], 7: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 4]]}
Explanation : Prints all possible combination of key with values and cross values as well.
Input : test_dict = {“Gfg” : [4], “is” : [1], “Best” : [4]}
Output : {0: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 4]]}
Explanation : Prints all possible combination of key with values and cross values as well.
Method #1 : Using product() + zip() + loop
The combination of above functions can be used to solve this problem. In this, we perform the first combination of keys with all values using product() and cross key combinations are performed using zip() and loop.
Python3
from itertools import product
test_dict = { "Gfg" : [ 4 , 5 , 7 ],
"is" : [ 1 , 2 , 9 ],
"Best" : [ 9 , 4 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
temp = list (test_dict.keys())
res = dict ()
cnt = 0
for combs in product ( * test_dict.values()):
res[cnt] = [[ele, cnt] for ele, cnt in zip (test_dict, combs)]
cnt + = 1
print ( "The computed combinations : " + str (res))
|
Output
The original dictionary is : {‘Gfg’: [4, 5, 7], ‘is’: [1, 2, 9], ‘Best’: [9, 4, 2]} The computed combinations : {0: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 9]], 1: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 4]], 2: [[‘Gfg’, 4], [‘is’, 1], [‘Best’, 2]], 3: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 9]], 4: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 4]], 5: [[‘Gfg’, 4], [‘is’, 2], [‘Best’, 2]], 6: [[‘Gfg’, 4], [‘is’, 9], [‘Best’, 9]], 7: [[‘Gfg’, 4], [‘is’, 9], [‘Best’, 4]], 8: [[‘Gfg’, 4], [‘is’, 9], [‘Best’, 2]], 9: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 9]], 10: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 4]], 11: [[‘Gfg’, 5], [‘is’, 1], [‘Best’, 2]], 12: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 9]], 13: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 4]], 14: [[‘Gfg’, 5], [‘is’, 2], [‘Best’, 2]], 15: [[‘Gfg’, 5], [‘is’, 9], [‘Best’, 9]], 16: [[‘Gfg’, 5], [‘is’, 9], [‘Best’, 4]], 17: [[‘Gfg’, 5], [‘is’, 9], [‘Best’, 2]], 18: [[‘Gfg’, 7], [‘is’, 1], [‘Best’, 9]], 19: [[‘Gfg’, 7], [‘is’, 1], [‘Best’, 4]], 20: [[‘Gfg’, 7], [‘is’, 1], [‘Best’, 2]], 21: [[‘Gfg’, 7], [‘is’, 2], [‘Best’, 9]], 22: [[‘Gfg’, 7], [‘is’, 2], [‘Best’, 4]], 23: [[‘Gfg’, 7], [‘is’, 2], [‘Best’, 2]], 24: [[‘Gfg’, 7], [‘is’, 9], [‘Best’, 9]], 25: [[‘Gfg’, 7], [‘is’, 9], [‘Best’, 4]], 26: [[‘Gfg’, 7], [‘is’, 9], [‘Best’, 2]]}
Time complexity: O(k^n), where n is the number of keys in the input dictionary and k is the maximum number of values associated with a key. This is because the program generates all possible combinations of values, which is equivalent to taking the Cartesian product of the value lists. The number of possible combinations is equal to k^n.
Auxiliary space: O(k^n), where n is the number of keys in the input dictionary and k is the maximum number of values associated with a key. This is because the program creates a dictionary to store the resulting key-value combinations, with one entry for each possible combination. The number of possible combinations is equal to k^n, so the resulting dictionary can have a maximum size of k^n.
Method #2 : Using product() + loop
The combination of above functions can also be used to solve this problem. In this, we perform the task of performing inner and cross keys combination using product(). Difference is that container of grouping is tuple rather than list.
Python3
from itertools import product
test_dict = { "Gfg" : [ 4 , 5 , 7 ],
"is" : [ 1 , 2 , 9 ],
"Best" : [ 9 , 4 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = {}
for key, val in test_dict.items():
res[key] = product([key], val)
res = product( * res.values())
print ( "The computed combinations : " + str ( list (res)))
|
Output
The original dictionary is : {‘Gfg’: [4, 5, 7], ‘is’: [1, 2, 9], ‘Best’: [9, 4, 2]} The computed combinations : [((‘Gfg’, 4), (‘is’, 1), (‘Best’, 9)), ((‘Gfg’, 4), (‘is’, 1), (‘Best’, 4)), ((‘Gfg’, 4), (‘is’, 1), (‘Best’, 2)), ((‘Gfg’, 4), (‘is’, 2), (‘Best’, 9)), ((‘Gfg’, 4), (‘is’, 2), (‘Best’, 4)), ((‘Gfg’, 4), (‘is’, 2), (‘Best’, 2)), ((‘Gfg’, 4), (‘is’, 9), (‘Best’, 9)), ((‘Gfg’, 4), (‘is’, 9), (‘Best’, 4)), ((‘Gfg’, 4), (‘is’, 9), (‘Best’, 2)), ((‘Gfg’, 5), (‘is’, 1), (‘Best’, 9)), ((‘Gfg’, 5), (‘is’, 1), (‘Best’, 4)), ((‘Gfg’, 5), (‘is’, 1), (‘Best’, 2)), ((‘Gfg’, 5), (‘is’, 2), (‘Best’, 9)), ((‘Gfg’, 5), (‘is’, 2), (‘Best’, 4)), ((‘Gfg’, 5), (‘is’, 2), (‘Best’, 2)), ((‘Gfg’, 5), (‘is’, 9), (‘Best’, 9)), ((‘Gfg’, 5), (‘is’, 9), (‘Best’, 4)), ((‘Gfg’, 5), (‘is’, 9), (‘Best’, 2)), ((‘Gfg’, 7), (‘is’, 1), (‘Best’, 9)), ((‘Gfg’, 7), (‘is’, 1), (‘Best’, 4)), ((‘Gfg’, 7), (‘is’, 1), (‘Best’, 2)), ((‘Gfg’, 7), (‘is’, 2), (‘Best’, 9)), ((‘Gfg’, 7), (‘is’, 2), (‘Best’, 4)), ((‘Gfg’, 7), (‘is’, 2), (‘Best’, 2)), ((‘Gfg’, 7), (‘is’, 9), (‘Best’, 9)), ((‘Gfg’, 7), (‘is’, 9), (‘Best’, 4)), ((‘Gfg’, 7), (‘is’, 9), (‘Best’, 2))]
Time complexity: O(n^m), where n is the maximum length of the value lists in the dictionary, and m is the number of key-value pairs in the dictionary.
Auxiliary space: O(n^m), as it creates a new dictionary res and stores the product of key-value combinations for each key, and then computes the product of all combinations using itertools.product(). The final result is stored as a list in memory.
Method 3: Using product() + nested list comprehension + dictionary comprehension
This approach directly creates the list of key-value pairs using nested list comprehension, and then uses dictionary comprehension to create a dictionary of product values for each key. Finally, it computes the cross key combinations and prints the result.
Python3
from itertools import product
test_dict = { "Gfg" : [ 4 , 5 , 7 ],
"is" : [ 1 , 2 , 9 ],
"Best" : [ 9 , 4 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res_list = [[key, val] for key, val in test_dict.items()]
res_dict = {key: product([key], val) for key, val in res_list}
res = product( * res_dict.values())
print ( "The computed combinations : " + str ( list (res)))
|
Output
...Best', 9)), (('Gfg', 4), ('is', 1), ('Best', 4)), (('Gfg', 4), ('is', 1), ('Best', 2)), (('Gfg', 4), ('is', 2), ('Best', 9)), (('Gfg', 4), ('is', 2), ('Best', 4)), (('Gfg', 4), ('is', 2), ('Best', 2)), (('Gfg', 4), ('is', 9), ('Best', 9)), (('Gfg', 4), ('is', 9), ('Best', 4)), (('Gfg', 4), ('is', 9), ('Best', 2)), (('Gfg', 5), ('is', 1), ('Best', 9)), (('Gfg', 5), ('is', 1), ('Best', 4)), (('Gfg', 5), ('is', 1), ('Best', 2)), (('Gfg', 5), ('is', 2), ('Best', 9)), (('Gfg', 5), ('is', 2), ('Best', 4)), (('Gfg', 5), ('is', 2), ('Best', 2)), (('Gfg', 5), ('is', 9), ('Best', 9)), (('Gfg', 5), ('is', 9), ('Best', 4)), (('Gfg', 5), ('is', 9), ('Best', 2)), (('Gfg', 7), ('is', 1), ('Best', 9)), (('Gfg', 7), ('is', 1), ('Best', 4)), (('Gfg', 7), ('is', 1), ('Best', 2)), (('Gfg', 7), ('is', 2), ('Best', 9)), (('Gfg', 7), ('is', 2), ('Best', 4)), (('Gfg', 7), ('is', 2), ('Best', 2)), (('Gfg', 7), ('is', 9), ('Best', 9)), (('Gfg', 7), ('is', 9), ('Best', 4)), (('Gfg', 7), ('is', 9), ('Best', 2))]
Time complexity: O(k^n), where n is the number of keys in the input dictionary and k is the maximum number of values associated with a key
Auxiliary space: O(k^n)
Method #4: Using itertools.combinations() and itertools.product()
This code first generates all possible combinations of two keys using the combinations() function. Then, for each key combination, it generates all possible value combinations using the product() function. Finally, it creates a list of dictionaries, where each dictionary contains a key-value pair from each key combination.
Python3
from itertools import combinations, product
test_dict = { "Gfg" : [ 4 , 5 , 7 ],
"is" : [ 1 , 2 , 9 ],
"Best" : [ 9 , 4 , 2 ]}
print ( "The original dictionary is : " + str (test_dict))
res = []
for k1, k2 in combinations(test_dict.keys(), 2 ):
for v1, v2 in product(test_dict[k1], test_dict[k2]):
res.append({k1: v1, k2: v2})
print ( "The computed combinations : " + str (res))
|
Output
The original dictionary is : {'Gfg': [4, 5, 7], 'is': [1, 2, 9], 'Best': [9, 4, 2]}
The computed combinations : [{'Gfg': 4, 'is': 1}, {'Gfg': 4, 'is': 2}, {'Gfg': 4, 'is': 9}, {'Gfg': 5, 'is': 1}, {'Gfg': 5, 'is': 2}, {'Gfg': 5, 'is': 9}, {'Gfg': 7, 'is': 1}, {'Gfg': 7, 'is': 2}, {'Gfg': 7, 'is': 9}, {'Gfg': 4, 'Best': 9}, {'Gfg': 4, 'Best': 4}, {'Gfg': 4, 'Best': 2}, {'Gfg': 5, 'Best': 9}, {'Gfg': 5, 'Best': 4}, {'Gfg': 5, 'Best': 2}, {'Gfg': 7, 'Best': 9}, {'Gfg': 7, 'Best': 4}, {'Gfg': 7, 'Best': 2}, {'is': 1, 'Best': 9}, {'is': 1, 'Best': 4}, {'is': 1, 'Best': 2}, {'is': 2, 'Best': 9}, {'is': 2, 'Best': 4}, {'is': 2, 'Best': 2}, {'is': 9, 'Best': 9}, {'is': 9, 'Best': 4}, {'is': 9, 'Best': 2}]
Time complexity: O(n^2 * m^2), where n is the number of keys in the dictionary and m is the average length of the value lists. The combinations() function generates n(n-1)/2 key combinations, and the product() function generates m^2 value combinations for each key combination. Therefore, the total number of key-value combinations generated is n(n-1)/2 * m^2.
Auxiliary space: O(n^2 * m^2), because we are storing all key-value combinations in a list. The size of the list is n(n-1)/2 * m^2.
Similar Reads
Python | Dictionary key combinations
Sometimes, while working with Python dictionaries, we can have a problem in which we need to get all the possible pair combinations of dictionary pairs. This kind of application can occur in the data science domain. Let's discuss certain ways in which this task can be performed. Method #1: Using lis
5 min read
Python - Dictionary values combination of size K
Sometimes while working with Python dictionaries, we can have a problem in which we need to extract combination of certain key's value of size K. This is usually when values are in form of strings. This can have application in day-day programming. Let's discuss a way in which this task can be perfor
4 min read
Python - Common items Dictionary Value List
The functionality of union has been discussed many times. But sometimes, we can have a more complex container, in which we need to check for the intersection of lists which are in form of keys of dictionary. Letâs discuss certain ways to solve this type of problem. Method #1: Using Loops Using loops
10 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 | Combining values from dictionary of list
Given a dictionary of list values, the task is to combine every key-value pair in every combination. Input : {"Name" : ["Paras", "Chunky"], "Site" : ["Geeksforgeeks", "Cyware", "Google"] } Output: [{'Site': 'Geeksforgeeks', 'Name': 'Paras'}, {'Site': 'Cyware', 'Name': 'Paras'}, {'Site': 'Google', 'N
2 min read
Python - All combination Dictionary List
Given 2 lists, form all possible combination of dictionaries that can be formed by taking keys from list1 and values from list2. Input : test_list1 = ["Gfg", "is", "Best"], test_list2 = [4] Output : [{'Gfg': 4, 'is': 4, 'Best': 4}]Explanation : All combinations dictionary list extracted. Input : tes
3 min read
Python - Sort Dictionary key and values List
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the sorting of it, wrt keys, but also can have a variation in which we need to perform a sort on its values list as well. Let's discuss certain way in which this task can be performed. Input : test_d
6 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 - Convert key-values list to flat dictionary
We are given a list that contains tuples with the pairs of key and values we need to convert that list into a flat dictionary. For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age':
3 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