In this python dictionary program, we will create key-value list pairings in a given dictionary
Steps to solve the program
- Take a dictionary in the given format as input.
- From itertools import product.
- Use for loop and pass the values to the product() function.
- Using the zip() function combine the key and its respective value as a single key-value pair.
- Convert these pairs into a dictionary using dict() and store the result in a variable.
- Print the dictionary inside a list.
from itertools import product
D1 = {1:['Virat Kohli'],2:['Rohit Sharma'],3:['Hardik Pandya']}
for sub in product(*D1.values()):
result = [dict(zip(D1, sub))]
print(result)
Output :
[{1: 'Virat Kohli', 2: 'Rohit Sharma', 3: 'Hardik Pandya'}]