跳到主要内容

Python 字典 popitem() 方法


描述

Python 字典 popitem() 方法返回并删除字典中的最后一对键和值。

如果字典已经为空,却调用了此方法,就报出 KeyError 异常。

语法

popitem()方法语法:

popitem()

参数

返回值

返回一个键值对(key,value)形式。

示例

以下实例展示了 popitem() 方法的使用方法:

示例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-

site= {'name': '集码教程', 'alexa': 10000, 'url': 'www.lectcode.com'}
pop_obj=site.popitem()
print pop_obj
print site

输出结果为:

('url', 'www.lectcode.com')
{'alexa': 10000, 'name': '\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'}