Question:

Consider the following dictionaries, D and D1:
D = { "Suman": 40, "Raj": 55, "Raman": 60 }
D1 = { "Aditi": 30, "Amit": 90, "Raj": 20 }

(Answer using built-in Python functions only)

(i)
(a) Write a statement to display/return the value corresponding to the key "Raj" in the dictionary D.

OR

(b) Write a statement to display the length of the dictionary D1.

(ii)
(a) Write a statement to append all the key-value pairs of the dictionary D to the dictionary D1.

OR

(b) Write a statement to delete the item with the given key "Amit" from the dictionary D1.

Show Hint

Use update() to merge dictionaries,
pop() to remove an item, len() to find size,
and square brackets [] to access values by key.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

(i) (a) To get the value for "Raj":
print(D["Raj"])
This statement accesses the value mapped to the key "Raj"
and displays it using print().
OR
(i) (b) To get the length of D1:
print(len(D1))
This statement uses the built-in len() function
to count the number of key-value pairs in D1 and display it.
(ii) (a) To merge D into D1:
D1.update(D)
The update() method adds all key-value pairs
from D to D1. If a key already exists,
its value will be updated with the value from D.
OR
(ii) (b) To delete "Amit" from D1:
D1.pop("Amit")
The pop() method removes the item with the given key
from the dictionary D1.
Was this answer helpful?
0
0