Python program to replace dictionary values with their average.

In this python dictionary program, we will replace dictionary values with their average.

Steps to solve the program
  1. Take a dictionary in a list as input.
  2. Use for loop to iterate over the dictionary.
  3. Create two variables and assign their values equal to the values of the respective keys using pop().
  4. Create a new key and assign its value equal to the average of the above two variables.
  5. Add the above key to the dictionary.
  6. 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}]
				
			

sort items in a dictionary in descending order.

match key values in two dictionaries.

Leave a Comment