Open In App

Python – Dictionary Key Value lists combinations

Last Updated : 08 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Dictionary Key Value lists combinations
# Using product() + zip() + loop
from itertools import product
 
# initializing dictionary
test_dict = {"Gfg" : [4, 5, 7],
             "is" : [1, 2, 9],
             "Best" : [9, 4, 2]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
temp = list(test_dict.keys())       
res = dict()
cnt = 0
 
# making key-value combinations using product
for combs in product (*test_dict.values()):
      
    # zip used to perform cross keys combinations.
    res[cnt] = [[ele, cnt] for ele, cnt in zip(test_dict, combs)]
    cnt += 1
 
# printing result
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




# Python3 code to demonstrate working of
# Dictionary Key Value lists combinations
# Using product() + loop
from itertools import product
 
# initializing dictionary
test_dict = {"Gfg" : [4, 5, 7],
             "is" : [1, 2, 9],
             "Best" : [9, 4, 2]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
res = {}
for key, val in test_dict.items():
     
    # for key-value combinations
    res[key] = product([key], val)
 
# computing cross key combinations
res = product(*res.values())
 
# printing result
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




# Python3 code to demonstrate working of
# Dictionary Key Value lists combinations
# Using product() + nested list comprehension + dictionary comprehension
from itertools import product
 
# initializing dictionary
test_dict = {"Gfg": [4, 5, 7],
             "is": [1, 2, 9],
             "Best": [9, 4, 2]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# creating list of key-value combinations using nested list comprehension
res_list = [[key, val] for key, val in test_dict.items()]
 
# creating dictionary of key-value combinations using dictionary comprehension
res_dict = {key: product([key], val) for key, val in res_list}
 
# computing cross key combinations
res = product(*res_dict.values())
 
# printing result
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
 
# initializing dictionary
test_dict = {"Gfg" : [4, 5, 7],
             "is" : [1, 2, 9],
             "Best" : [9, 4, 2]}
 
# printing original dictionary
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})
 
# printing result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads