Computer Science

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

Python Dictionaries

1 Like

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'

Answered By

1 Like


Related Questions