Python 字典 popitem() 方法
描述
Python 字典 popitem() 方法返回並刪除字典中的最後一對鍵和值。
如果字典已經為空,卻調用了此方法,就報出 KeyError 異常。
語法
popitem()方法語法:
popitem()
參數
- 無
返回值
返回一個鍵值對(key,value)形式。
實例
以下實例展示了 popitem() 方法的使用方法:
實例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
site= {'name': 'IT研修', 'alexa': 10000, 'url': 'www.xuhuhu.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)
# -*- coding: UTF-8 -*-
site= {'name': 'IT研修', 'alexa': 10000, 'url': 'www.xuhuhu.com'}
pop_obj=site.popitem()
print(pop_obj)
print(site)
輸出結果為:
('url', 'www.xuhuhu.com') {'alexa': 10000, 'name': '\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'}