Dremendo Tag Line

Update values of an old dictionary from a new dictionary in Python

Dictionaries - Question 6

In this question, we will see how to update the marks of the subjects given in dictionary a by the marks of the subjects given in dictionary b in Python programming. To know more about dictionaries click on the dictionaries lesson.

Two dictionaries are given below as:

    a={'english':56, 'maths':84, 'physics':92, 'computer':76, 'history':87, 'geography':64}
    b={'maths':97, 'computer':94, 'geography':71}

Q6) Write a program in Python to update the marks of the subjects given in dictionary a by the marks of the subjects given in dictionary b. The output should be:

    {'english':56, 'maths':97, 'physics':92, 'computer':94, 'history':87, 'geography':71}

Program

a={'english':56, 'maths':84, 'physics':92, 'computer':76, 'history':87, 'geography':64}
b={'maths':97, 'computer':94, 'geography':71}

print('Dictionary a:',a)
print('Dictionary b:',b)

a.update(b)
print('Dictionary a after marks update')
print(a)

Output

Dictionary a: {'english': 56, 'maths': 84, 'physics': 92, 'computer': 76, 'history': 87, 'geography': 64}
Dictionary b: {'maths': 97, 'computer': 94, 'geography': 71}
Dictionary a after marks update
{'english': 56, 'maths': 97, 'physics': 92, 'computer': 94, 'history': 87, 'geography': 71}
video-poster