In this python dictionary program, we will create a nested dictionary using a list.
Steps to solve the program
- Take a list and a dictionary as input and create an empty dictionary.
- Use for loop to iterate over a combination of list and dictionary using zip().
- The zip () function will combine the first element of the list with the first key-value pair of the dictionary.
- Add the list element as the key and the key-value pair from the dictionary as its value to the empty dictionary.
- 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}}