Python3 字典 popitem() 方法
描述
Python 字典 popitem() 方法隨機返回並刪除字典中的最後一對鍵和值。
如果字典已經為空,卻調用了此方法,就報出KeyError異常。
語法
popitem()方法語法:
popitem()
參數
- 無
返回值
返回一個鍵值對(key,value)形式,按照 LIFO(Last In First Out 後進先出法) 順序規則,即最末尾的鍵值對。
實例
以下實例展示了 popitem() 方法的使用方法:
實例
#!/usr/bin/python3
site= {'name': 'IT研修', 'alexa': 10000, 'url': 'www.xuhuhu.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)
site= {'name': 'IT研修', 'alexa': 10000, 'url': 'www.xuhuhu.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)
輸出結果為:
('url', 'www.xuhuhu.com') {'name': 'IT研修', 'alexa': 10000}