Python Dictionary popitem() Method

Python dictionary popitem() method is used to return and remove the last inserted element of the dictionary (dict). Before going to know about popitem() we have to know basic idea about Python dictionaries, dictionaries are a collection of items used for storing key- value pairs. They are mutable so, we can update the dictionary by adding new key-value pairs, removing existing key-value pairs from the dictionary, or changing the value of the corresponding key.

In this article, I will explain about Python dictionary popitem() method with examples. Using this method we can remove and return the last element of the dictionary. If the dictionary is empty, it returns a KeyError.

All dictionary methods are available on the dictionary methods page.

Quick Examples of Python Dictionary popitem()

Following are the quick examples of the Dictionary popitem() method.


# Example 1:  Remove last element using popitem()
technology = {'course': 'python', 'fee': 4000, 'duration': '45days'}
element=technology.popitem()

# Example 2: Use popitem() remove newly inserted element
technology = {'course': 'python', 'fee': 4000, 'duration': '45 days'}
technology['tutor']='Richard'
element=technology.popitem()

# Example 3: Return KeyError
technology = dict()
element = technology.popitem()
print(element)

1. Syntax of Python Dictionary popitem()

Following is the syntax of the popitem() method of the Python Dictionary.


# Syntax of the popitem()
dict.popitem()

1.2 Parameter of popitem()

Popitem() doesn’t allow any parameters.

1.3 Return Value of popitem()

It returns the last inserted element and removes it from the dictionary. This method follows the LIFO (Last In First Out) approach, so this function returns and removes the last pair inserted. 

4. Usage of Python Dictionary popitem() Method

popitem() is a built-in method in python dictionary that is used to remove and return the last key-value pair from the dict. It follows the Last In First Out approach. If the dictionary is empty, it returns a KeyError.


# Remove last item using popitem()
technology = {'course': 'python', 'fee': 4000, 'duration': '45 days'}
element=technology.popitem()
print("Removed element:",element)
print(technology)

# Output:
Removed element: ('duration', '45 days')
{'course': 'python', 'fee': 4000}
 

From the above code, the last element ‘duration’:’45 days’ has been removed from the Python dictionary and returned as a tuple.

5. Add Element & Remove From Dictionary

Any new element added to the dictionary will be inserted at the end, so using popitem() function remove this last inserted key-value. Let’s see with an example.


# Use popitem() remove newly inserted element
technology = {'course': 'python', 'fee': 4000, 'duration': '45 days'}
technology['tutor'] = 'Richard'
element = technology.popitem()
print("Removed element:",element)
print(technology)

# Output:
Removed element: ('tutor', 'Richard')
{'course': 'python', 'fee': 4000, 'duration': '45 days'}

In the above example, we have added 'tutor':'Richard' and by using popitem() we remove it from the dict.

6. Check The Empty Dictionary Using popitem()

If the dictionary is empty, this function returns an error KeyError.


# Create empty dictionary
technology = dict()
element = technology.popitem()
print(element)

# Output:
KeyError: 'popitem(): dictionary is empty'

7. Conclusion

In this article, I have explained about Python dictionary popitem() method, and by using this how we can remove the last element of the dictionary. As well as I have explained the behavior of popitem() when used on the empty dictionary.

Happy Learning !!

References

Related Articles

Leave a Reply

You are currently viewing Python Dictionary popitem() Method