Create key-value list pairings in a given dictionary

In this python dictionary program, we will create key-value list pairings in a given dictionary

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. From itertools import product.
  3. Use for loop and pass the values to the product() function.
  4. Using the zip() function combine the key and its respective value as a single key-value pair.
  5. Convert these pairs into a dictionary using dict() and store the result in a variable.
  6. 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'}]
				
			

count the frequency in a given dictionary.

get the total length of a dictionary with string values.

Leave a Comment