CBSE Class 11 Computer Science Question 30 of 112

Dictionaries — Question 10

Back to all questions
10
Question

Question 10

Which of the following will raise an error if the given dictionary is empty ?

  1. del statement
  2. pop( )
  3. popitem( )
  4. all of these
Answer

popitem()

Reason — Calling popitem() method on an empty dictionary will throw a KeyError.
For example:

d = {}
d.popitem()
Output

KeyError: 'popitem(): dictionary is empty'