Python Dictionary popitem() Method
Description
The Python Dictionary popitem() method returns and removes the last key-value pair in the dictionary.
If the dictionary is already empty when 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 (key, value).
Example
The following example shows how to use the popitem() method:
Example
#!/usr/bin/python
-- coding: UTF-8 --
site
=
{
'name'
:
'Runoob Tutorial'
,
'alexa'
:
10000
,
'url'
:
'www.runoob.com'
}
pop_obj
=
site
.
popitem
(
)
(
pop_obj
)
(
site
)
The output is:
('url', 'www.runoob.com')
{'alexa': 10000, 'name': '\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'}
Python Dictionary