字典删除
del
dict1={"张三":100,"李四":100,"王五":89,"赵柳":99}
del dict1["王五"]
print(dict1)
删除内置函数
pop()
通过key来删除
result=dict1.pop("李四",20)
print(result)
print(dict1)
result=dict1.pop("zhou",'39')
print(result)
100
{'张三': 100, '赵柳': 99}
39
popitem()
result=dict1.popitem()
print(result)
print(dict1)
clear()
dict1.clear()
print(dict1)
其他内置方法
update()
dict2={0:"tom",1:"zhou",2:"ss"}
dict3={0:"lily",3:"tom"}
dict2.update(dict3)
print(dict2)
{0: 'lily', 1: 'zhou', 2: 'ss', 3: 'tom'}
fromkeys
fromkeys(seq)将seq转成字典形式,如果没有默认value,则用代替None 情况一:未指定default值
list=['aa','bb','cc']
new_dict=dict.fromkeys(list)
print(new_dict)
{'aa': None, 'bb': None, 'cc': None}
情况二:指定default值
list=['aa','bb','cc']
new_dict=dict.fromkeys(list,10)
print(new_dict)
{'aa': 10, 'bb': 10, 'cc': 10}
|