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

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 return 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 dictionary popitem method is:-

dictionary.popitem()

Parameter

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

Return Value

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

Python dictionary popitem example

Here we will take some example to understand 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 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

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply