Python Dictionary popitem() Method

Rate this post

Related Article:

Definition

The Python method dict.popitem() removes and returns the last (key-value) pair element that was inserted in the dictionary.

Syntax

dict.popitem()

Parameters

  • The dict.popitem() method does not take any parameter inputs.

Return-Value

  • The dict.popitem() method returns a tuple containing the last (key-value) pair element of the dictionary. It also removes the last (key-value) pair element from the dictionary.

Example: Remove and Insert

Example on using the dict.popitem() method to remove and insert (key-value) pair elements:

employee = {'id': 1, 'full-name': 'bob keller', 
            'age': 30, 'yearly-salary': 50000, 
            'date-of-birth': '11/5/1985', 
            'profession': 'electrician'}
removed_profession = employee.popitem()

print('value returned: ', removed_profession)
print('updated employee profession: ', employee)
 
# inserting a new profession into the employee dictionary:
employee['profession'] = 'masonry'
print('employees new profession: ', employee)

Output:

value returned:  ('profession', 'electrician')
updated employee profession:  {'id': 1, 'full-name': 'bob keller', 'age': 30, 'yearly-salary': 50000, 'date-of-birth': '11/5/1985'}
employees new profession:  {'id': 1, 'full-name': 'bob keller', 'age': 30, 'yearly-salary': 50000, 'date-of-birth': '11/5/1985', 'profession': 'masonry'}

As the results show, applying the dict.popitem() method on a dictionary removes the last (key-value) pair element from a dictionary, in this case the (‘profession’: ‘electrician’) key-value pair element was removed from the dictionary, the removed element can also be stored in a variable.

This can be useful when there is a requirement to insert a new replacement item, like the new profession masonry.

Python dict.popitem() Empty Dictionary

If you apply the Python dict.popitem() method to an empty dictionary, you obtain the following output:

KeyError: 'popitem(): dictionary is empty'

When the popitem() method is applied to an empty dictionary a KeyError is returned, showing the message that the dictionary is empty.

LIFO Order of dict.popitem()

Example to show last-in-first-out order with the dict.popitem() method:

dict = {}
dict['id_1'] = 'sarah'
dict['id_2'] = 'tim'
dict['id_3'] = 'tammy'
 
id = dict.popitem()
print(id)
 
id = dict.popitem()
print(id)
 
id = dict.popitem()
print(id)

Output:

('id_3', 'tammy')
('id_2', 'tim')
('id_1', 'sarah')

This example shows that the last (key-value) pair element to be inserted into a Python dictionary will be the first element removed by the dict.popitem() method.

While Loop dicts.popitem()

Example using dict.popitem() method in a while loop:

grocery_items = {'bananas': 6, 'apples': 12, 'oranges': 8, 'kiwis': 9}
 
while grocery_items:
   grocery_item = grocery_items.popitem()
   print(grocery_item)
   print(grocery_items)

Output:

('kiwis', 9)
{'bananas': 6, 'apples': 12, 'oranges': 8}
('oranges', 8)
{'bananas': 6, 'apples': 12}
('apples', 12)
{'bananas': 6}
('bananas', 6)
{}

In this example a while loop is used to iterate through a grocery_items dictionary, removing the last (key-value) pair element 1-by-1 and storing them in a grocery_item variable.

When the original grocery_items dictionary is printed at the end, it’s empty because the dict.popitem() method removed all the (key-value) pair elements from the dictionary through the while-loop’s iterations.