Python 字典 pop() 方法
描述
Python 字典 pop() 方法刪除字典給定鍵 key 及對應的值,返回值為被刪除的值。key 值必須給出。 否則,返回 default 值。
語法
pop()方法語法:
pop(key[,default])
參數
- key: 要刪除的鍵值
- default: 如果沒有 key,返回 default 值
返回值
返回被刪除的值。
實例
以下實例展示了 pop() 方法的使用方法:
實例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
site= {'name': 'IT研修', 'alexa': 10000, 'url': 'www.xuhuhu.com'}
pop_obj=site.pop('name')
print pop_obj # 輸出 :IT研修
# -*- coding: UTF-8 -*-
site= {'name': 'IT研修', 'alexa': 10000, 'url': 'www.xuhuhu.com'}
pop_obj=site.pop('name')
print pop_obj # 輸出 :IT研修