Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Dictionary popitem() Method

Python Dictionary popitem() Method

Python Dictionary Python Dictionary


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

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

(

)

print

(

pop_obj

)

print

(

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 Python Dictionary