The popitem() method removes and returns the last item in the dictionary.

The syntax is as shown below:

d.popitem()

The method does not take any argument. It returns a two-length tuple in the form of (key, value) of the removed item.

d = {'Moscow': 'Russia',
      'Delhi': 'India',
      'Manilla': 'Philippines',
      'Nairobi': 'Kenya'
    }

#remove random items
print(d.popitem())
print(d.popitem())

#the remaining
print(d)

If the dictionary is empty, a KeyError exception will be raised.

d = {'Moscow': 'Russia', 
      'Delhi': 'India', 
    }

#remove random items
print(d.popitem())
print(d.popitem())
print(d.popitem()) #raises a KeyError