Show keys associated with values in dictionary.

In this python dictionary program, we will show keys associated with values in dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Use for loop to iterate over key-value pairs in the input dictionary.
  3. Use a nested for loop to iterate over elements in the values list.
  4. Add the element as the key and the key for that value list as its value to the empty dictionary.
  5. 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']}

				
			

Extract Unique values from dictionary values.

convert a list of dictionaries into a list of values corresponding to the specified key.

Leave a Comment