Consider the following Python dictionary: D = {1: 'Amar', 2: 'Akbar', 3:…
2026
Consider the following Python dictionary:
D = {1: 'Amar', 2: 'Akbar', 3: 'Anthony'}
Which of the following statements will perform an operation on dictionary D that is most similar to D.pop(2)?
- A.
del D(2)
- B.
del D[2]
- C.
D.popitem(2)
- D.
D.remove(2)
Attempted by 449 students.
Show answer & explanation
Correct answer: B
The correct answer is Option B (del D[2]).
The method D.pop(2) removes the key-value pair with key 2 from the dictionary and also returns its value.
The statement del D[2] performs a similar operation by deleting the key-value pair with key 2 from the dictionary (though it does not return the value).
Other options are incorrect due to invalid syntax or incorrect methods.
Thus, the closest equivalent to D.pop(2) is del D[2].