In this python dictionnary program, we will update the list values in the dictionary.
Steps to solve the program
- Take a dictionary in the given format as input.
- Create a function to update the values.
- Use for loop to update values for each key of the dictionary.
- Add 10 to the values for the 1st key and subtract 15 from the values of the 2nd key.
- Return the dictionary.
- 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]}