In this python dictionary program, we will replace dictionary values with their average.
Steps to solve the program
- Take a dictionary in a list as input.
- Use for loop to iterate over the dictionary.
- Create two variables and assign their values equal to the values of the respective keys using pop().
- Create a new key and assign its value equal to the average of the above two variables.
- Add the above key to the dictionary.
- Print the output.
dict1 = [{'name':'ketan','subject':'maths','p1':80,'p2':70}]
for d in dict1:
n1 = d.pop('p1')
n2 = d.pop('p2')
d['p1+p2'] = (n1 + n2)/2
print(dict1)
Output :
[{'name': 'ketan', 'subject': 'maths', 'p1+p2': 75.0}]