首页 › 参考手册 › 字典方法 › dict.fromkeys()

Python dict.fromkeys() 方法

描述

用可迭代对象中的元素作为键创建新字典,所有键的值都是 value。

语法

dict.fromkeys(iterable[, value])

参数

参数说明
iterable键
value统一的值,默认 None

返回值

新字典

实例

print(dict.fromkeys(["a", "b"], 0))
print(list(dict.fromkeys([3, 1, 3, 2])))   # 保序去重
以上实例输出结果为:
{'a': 0, 'b': 0}
[3, 1, 2]