Python语言基础
Python语言进阶
Python数据结构

Python dictionary popitem() 方法

搞懂Python dictionary popitem() 方法的用法

描述

Python 字典 popitem() 方法随机返回并删除字典中的最后一对键和值。
如果字典已经为空,却调用了此方法,就报出KeyError异常。

语法

popitem()方法语法:
popitem()

参数

返回值

返回一个键值对(key,value)形式,按照 LIFO(Last In First Out 后进先出法) 顺序规则,即最末尾的键值对。

实例

以下实例展示了 popitem() 方法的使用方法:
site = { 'name': 'lidihuo', 'url': 'www.lidihuo.com' }
pop_obj = site. popitem ()
print (pop_obj)
print (site)
输出结果为:
('url', 'www.lidihuo.com')
{'name': 'lidihuo'}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4