CBSE Class 12 Computer Science Question 43 of 57

Review of Python Basics — Question 43

Back to all questions
43
Question

Question 38

Write a Python script to concatenate the following dictionaries to create a new one:

d1 = {'A': 1, 'B': 2, 'C': 3}
d2 = {'D': 4 }

Output should be:

{'A': 1, 'B': 2, 'C': 3, 'D' : 4}
Solution
d1 = {'A': 1, 'B': 2, 'C': 3}
d2 = {'D': 4}
d1.update(d2)
print("Concatenated dictionary: ", d1)
Output
Concatenated dictionary:  {'A': 1, 'B': 2, 'C': 3, 'D': 4}
Answer

d1
=
{
'A'
:
1
,
'B'
:
2
,
'C'
:
3
}
d2
=
{
'D'
:
4
}
d1
.
update
(
d2
)
print
(
"Concatenated dictionary: "
,
d1
)
Output
Concatenated dictionary: {'A': 1, 'B': 2, 'C': 3, 'D': 4}