Python Dictionary popitem() Method
Description
Python Dictionary popitem() method returns and removes the last key-value pair in the dictionary.
If the dictionary is already empty and this method is called, a KeyError exception is raised.
Syntax
popitem() method syntax:
popitem()
Parameters
- None
Return Value
Returns a key-value pair in the form of (key, value).
Example
The following example demonstrates the usage of the popitem() method:
Example
#!/usr/bin/python
# -*- coding: UTF-8 -*-
site= {'name': 'Example', 'alexa': 10000, 'url': 'www.example.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)
# -*- coding: UTF-8 -*-
site= {'name': 'Example', 'alexa': 10000, 'url': 'www.example.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)
The output result is:
('url', 'www.example.com')
{'alexa': 10000, 'name': '\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'}
Other Extensions
Python Dictionary