Skip to content

Question 14

What is the output of the following code?

1
2
3
d = {1: 'en', 2: 'zh', 3: 'ms', 4: 'ta'}
d.pop(1)
print(d[1])
Hint

What does the function pop(1) imply when used with this dictionary?

Solution

Given d = {1: 'en', 2: 'zh', 3: 'ms', 4: 'ta'}, d.pop(1) essentially implies removal of the key-value pair with key 1 from d. Hence, if you try to access it afterwards (i.e., with print(d[1]) or simply typing d[1] in the IDLE console), you would obtain a KeyError.

Answer

Executing this code yields an error