In this python dictionary program, we will show keys associated with values in dictionary.
Steps to solve the program
- Take a dictionary as input and create an empty dictionary.
- Use for loop to iterate over key-value pairs in the input dictionary.
- Use a nested for loop to iterate over elements in the values list.
- Add the element as the key and the key for that value list as its value to the empty dictionary.
- Print the output.
D1 = {'xyz':[20,40],'abc':[10,30]}
D2 = {}
for k,v in D1.items():
for ele in v:
D2[ele] = [k]
print(D2)
Output :
{20: ['xyz'], 40: ['xyz'], 10: ['abc'], 30: ['abc']}