Menu Close

Python Dictionary popitem() Method

In this dictionary method tutorial, you will learn all about the Python dictionary popitem() method to remove the last inserted item from the dictionary.

In the previous tutorial, we have seen all about the dictionary pop method to remove any particular item from the dictionary.

To understand this example, you should have basic knowledge of Python Dictionary.

Python Dictionary popitem() Method

The python dictionary popitem method is a dictionary built-in method that is used to remove the last inserted item from the dictionary. popitem function in Python returns the removed key and value as a tuple.

Note:- Before Python 3.7, popitem method remove any random item from the dictionary.

Syntax

The syntax of the dictionary popitem method is:-

dictionary.popitem()

Parameter

Dictionary popitem function in Python does not accept any parameter:-

Return Value

The return value of popitem method is deleted key-value as a tuple.

Python dictionary popitem example

Here we will take some examples to understand the dictionary popitem function in Python.

Example 1:


student = {'first_name': 'Vishvajit', 'last_name': 'Rao', 'course': 'BCA'}
result = student.popitem()
print(result)

Output will be:- (‘course’, ‘BCA’)

Conclusion

In this tutorial, you have learned all about the Python dictionary popitem method to remove the last inserted item from the dictionary. This is the best way when you want to remove the last item from the dictionary.

If this article helped you, please keep visiting for further interesting python tutorial.

Dictionary method


For more information:- Click Here

Python Dictionary update Method
Python Dictionary get Method

Related Posts