Update the list values in the dictionary.

In this python dictionnary program, we will update the list values in the dictionary.

Steps to solve the program
  1. Take a dictionary in the given format as input.
  2. Create a function to update the values.
  3. Use for loop to update values for each key of the dictionary.
  4. Add 10 to the values for the 1st key and subtract 15 from the values of the 2nd key.
  5. Return the dictionary.
  6. Now pass the input dictionary to the function to print the output.
				
					def add(dictionary):
    dictionary['virat'] = [x+10 for x in dictionary['virat']]
    dictionary['rohit'] = [x-25 for x in dictionary['rohit']]
    return dictionary

D1 = {'virat':[50,30],'rohit':[40,10]} 

print(add(D1))
				
			

Output :

				
					{'virat': [60, 40], 'rohit': [15, -15]}
				
			

clear the list values in the said dictionary.

extract a list of values from a given list of dictionaries

Leave a Comment