Python 字典 Dictionary popitem() 方法
Python 字典 popitem() 方法返回并删除字典中的最后一对键和值。
如果字典已经为空,却调用了此方法,就报出 KeyError 异常。
语法
popitem()方法语法:
popitem()
参数
- 无
返回值
返回一个键值对(key,value)形式。
实例
以下实例展示了 popitem() 方法的使用方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- site= {'name': '编程宝库', 'alexa': 10000, 'url': 'www.codebaoku.com'} pop_obj=site.popitem() print(pop_obj) print(site)
输出结果为:
('url', 'www.codebaoku.com') {'alexa': 10000, 'name': '编程宝库'}
发表评论