Program to create nested Dictionary using a list

In this python dictionary program, we will create a nested dictionary using a list.

Steps to solve the program
  1. Take a list and a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over a combination of list and dictionary using zip().
  3. The zip () function will combine the first element of the list with the first key-value pair of the dictionary.
  4. Add the list element as the key and the key-value pair from the dictionary as its value to the empty dictionary.
  5. Print the output.
				
					D1 = {'sqatools':8,'python':6}
l = [1,2]

res = {}
for key, ele in zip(l, D1.items()):
    res[key] = dict([ele])
    
print(res)
				
			

Output :

				
					{1: {'sqatools': 8}, 2: {'python': 6}}
				
			

get the depth of the dictionary.

extract key’s value if key is present in list and dictionary

Leave a Comment