Dremendo Tag Line

Merge two dictionaries in a new dictionary in Python

Dictionaries - Question 3

In this question, we will see how to merge two given dictionaries in a new dictionary. in Python programming. To know more about dictionaries click on the dictionaries lesson.

Two dictionaries are given below as:

    a={'name':'Henry', 'roll':1, 'class':8}
    b={'maths':84, 'physics':92, 'computer':76}

Q3) Write a program in Python to merge the above two dictionaries in a new dictionary. The output should be:

    {'name': 'Henry', 'roll': 1, 'class': 8, 'maths': 84, 'physics': 92, 'computer': 76}

Program

a={'name':'Henry', 'roll':1, 'class':8}
b={'maths':84, 'physics':92, 'computer':76}

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

c=dict()
c.update(a)
c.update(b)

print('Dictionary c:',c)

Output

Dictionary a: {'name': 'Henry', 'roll': 1, 'class': 8}
Dictionary b: {'maths': 84, 'physics': 92, 'computer': 76}
Dictionary c: {'name': 'Henry', 'roll': 1, 'class': 8, 'maths': 84, 'physics': 92, 'computer': 76}
video-poster