Python Dictionary popitem()

Python dictionary popitem() method

Python dictionary popitem() method removes the last item inserted from the dictionary, and returns the removed item.

In Python versions before 3.7, popitem() removes a random item from the dictionary.

In this tutorial, you will learn about dictionary popitem() method in Python, and its usage, with the help of example programs.

Syntax of dict popitem()

The syntax of dictionary popitem() method is

dictionary.popitem()

Parameters

The dictionary popitem() method does not take any parameters.

Return value

The dictionary popitem() method returns the removed item from dictionary as a tuple object.

Examples for popitem() method

In the following examples, we shall use Python 3.11.4. So, popitem() method shall remove the last inserted item from the dictionary.

1. Removing last inserted item from dictionary using popitem() method in Python

In this example, we are given a dictionary in my_dict with some initial key:value pairs. We have to remove the last inserted item from this dictionary.

Call popitem() method on the given dictionary object my_dict. We shall print the remove item, and also print the modified dictionary, to output.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

removed_item = my_dict.popitem()

print(f"Removed item : {removed_item}")
print(my_dict)
Run Code Copy

Since the last item in the initial values is the last inserted entry into the dictionary, the last item shall be removed and returned.

Output

Removed item : ('moo', 16)
{'foo': 12, 'bar': 14}

2. Remove all the items from dictionary using popitem()

In this example, we shall call popitem() method on the dictionary object repeatedly in a For loop, until the dictionary is empty. We shall print the returned item by popitem() method to the standard output.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

while my_dict:
    removed_item = my_dict.popitem()
    print(f"Removed item : {removed_item}")
else:
    print("The dictionary is emptied.")
Run Code Copy

The while loop executes until the given dictionary is empty. When the dictionary becomes empty, else-block executes.

Output

Removed item : ('moo', 16)
Removed item : ('bar', 14)
Removed item : ('foo', 12)
The dictionary is emptied.

3. Adding items to dictionary and then calling popitem() method

In this example, we are given a dictionary in my_dict with some initial key:value pairs. We shall add two more entries into the dictionary. We shall then call the popitem() on the dictionary. popitem() method must remove the last inserted entry.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

my_dict['poo'] = 18
my_dict['woo'] = 20

removed_item = my_dict.popitem()

print(f"Removed item : {removed_item}")
print(my_dict)
Run Code Copy

Since 'woo': 20 is the last item that is inserted to the dictionary, popitem() shall remove this entry from dictionary.

Output

Removed item : ('woo', 20)
{'foo': 12, 'bar': 14, 'moo': 16, 'poo': 18}

Summary

In this tutorial of Python Dictionary Methods, we learned how to use Dictionary popitem() method to remove the last inserted entry from the dictionary, with help of well detailed Python programs.

Related Tutorials

Code copied to clipboard successfully 👍