Python – All combination Dictionary List

11 Oct, 2020
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 : test_list1 = [“Gfg”, “is”, “Best”], test_list2 = [5, 6] Output : [{‘Gfg’: 5, ‘is’: 5, ‘Best’: 5}, {‘Gfg’: 5, ‘is’: 5, ‘Best’: 6}, {‘Gfg’: 5, ‘is’: 6, ‘Best’: 5}, {‘Gfg’: 5, ‘is’: 6, ‘Best’: 6}, {‘Gfg’: 6, ‘is’: 5, ‘Best’: 5}, {‘Gfg’: 6, ‘is’: 5, ‘Best’: 6}, {‘Gfg’: 6, ‘is’: 6, ‘Best’: 5}, {‘Gfg’: 6, ‘is’: 6, ‘Best’: 6}] Explanation : All combinations dictionary list extracted.  
Method #1 : Using product() + dictionary comprehension + list comprehension
In this, we get all the combination possible using product() and dictionary comprehension generates each dictionary, list comprehension is used to iterate combinations generated.
Python3
# Python3 code to demonstrate working of # All combination Dictionary List# Using product() + list comprehension + dictionary comprehensionfrom itertools import product  # initializing liststest_list1 = ["Gfg", "is", "Best"]test_list2 = [4, 5]  # printing original listsprint("The original list 1 is : " + str(test_list1))print("The original list 2 is : " + str(test_list2))  # generating combinationstemp = product(test_list2, repeat = len(test_list1))  # constructing dicts using combinationsres = [{key : val for (key , val) in zip(test_list1, ele)} for ele in temp]  # printing result print("The combinations dictionary : " + str(res))
Output:
The original list 1 is : [‘Gfg’, ‘is’, ‘Best’]The original list 2 is : [4, 5]The combinations dictionary : [{‘Gfg’: 4, ‘is’: 4, ‘Best’: 4}, {‘Gfg’: 4, ‘is’: 4, ‘Best’: 5}, {‘Gfg’: 4, ‘is’: 5, ‘Best’: 4}, {‘Gfg’: 4, ‘is’: 5, ‘Best’: 5}, {‘Gfg’: 5, ‘is’: 4, ‘Best’: 4}, {‘Gfg’: 5, ‘is’: 4, ‘Best’: 5}, {‘Gfg’: 5, ‘is’: 5, ‘Best’: 4}, {‘Gfg’: 5, ‘is’: 5, ‘Best’: 5}]
Method #2 : Using permutations() + dictionary comprehension + list comprehension [ For unique elements ]
If we require values to be all unique, permutations() can be used inplace of product() to achieve this task.
Python3
# Python3 code to demonstrate working of# All combination Dictionary List# Using product() + list comprehension + dictionary comprehensionfrom itertools import permutations  # initializing liststest_list1 = ["Gfg", "is", "Best"]test_list2 = [4, 5, 6]  # printing original listsprint("The original list 1 is : " + str(test_list1))print("The original list 2 is : " + str(test_list2))  # generating combinationstemp = list(permutations(test_list2, len(test_list1)))  # constructing dicts using combinationsres = [{key: val for (key, val) in zip(test_list1, ele)} for ele in temp]  # printing resultprint("The combinations dictionary : " + str(res))
Output:
The original list 1 is : [‘Gfg’, ‘is’, ‘Best’]The original list 2 is : [4, 5, 6]The combinations dictionary : [{‘Gfg’: 4, ‘is’: 5, ‘Best’: 6}, {‘Gfg’: 4, ‘is’: 6, ‘Best’: 5}, {‘Gfg’: 5, ‘is’: 4, ‘Best’: 6}, {‘Gfg’: 5, ‘is’: 6, ‘Best’: 4}, {‘Gfg’: 6, ‘is’: 4, ‘Best’: 5}, {‘Gfg’: 6, ‘is’: 5, ‘Best’: 4}]
Python dictionary-programs
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate through Excel rows in Python?
Deque in Python
Defaultdict in Python
Queue in Python
Rotate axis tick labels in Seaborn and Matplotlib
Defaultdict in Python
Python program to add two numbers
Python | Get dictionary keys as a list
Python Program for Fibonacci numbers
Python Program for factorial of a number