Python3 Att Dictionary Popitem
# Python3.x Python3 Dictionary popitem() Method
[ Python3 Dictionary](#)
* * *
## Description
The Python dictionary `popitem()` method randomly returns and removes the last inserted key-value pair from the dictionary.
If the dictionary is already empty and this method is called, a `KeyError` exception is raised.
## Syntax
The syntax for the `popitem()` method is:
popitem()
## Parameters
* None
## Return Value
Returns the last inserted key-value pair (in key, value form), following the LIFO (Last In First Out) order rule, i.e., the last key-value pair.
**Note:** Before Python 3.7, the `popitem()` method deleted and returned an arbitrary key-value pair inserted into the dictionary.
## Example
The following example demonstrates the usage of the `popitem()` method:
## Example
#!/usr/bin/python3
site={'name': '','alexa': 10000,'url': 'www.'}
# ('url': 'www.') is last inserted and will be deleted
result =site.popitem()
print('Return value = ', result)
print('site = ',site)
# Insert a new element
site['nickname']=''
print('site = ',site)
# Now ('nickname', '') is the last inserted element
result =site.popitem()
print('Return value = ', result)
print('site = ',site)
The output result is:
Return value = ('url', 'www.') site = {'name': '', 'alexa': 10000} site = {'name': '', 'alexa': 10000, 'nickname': ''}Return value = ('nickname', '') site = {'name': '', 'alexa': 10000}
* * Python3 Dictionary](#)
YouTip