Python Dictionary popitem()

The popitem() returns and removes an arbitrary element (key, value) pair from the dictionary.

The syntax of popitem() is:


popitem() Parameters

The popitem() doesn't take any parameters.

Return Value from popitem()

The popitem()

  • returns an arbitrary element (key, value) pair from the dictionary
  • removes an arbitrary element (the same element which is returned) from the dictionary.

Note: Arbitrary elements and random elements are not same. The popitem() doesn't return a random element. 

Example: How popitem() works?

person = {'name': 'Phill', 'age': 22, 'salary': 3500.0}

result = person.popitem()
print('person = ',person)
print('Return Value = ',result)

When you run the program, the output will be:

person =  {'name': 'Phill', 'salary': 3500.0}
result =  ('age', 22)

The popitem() raises a KeyError error if the dictionary is empty.