首页 › 参考手册 › 内置函数 › dict()

Python dict() 函数

描述

创建字典。可以传入关键字参数、另一个映射,或由键值对组成的可迭代对象。

语法

dict(**kwargs) / dict(mapping) / dict(iterable)

参数

参数说明
kwargs关键字参数
mapping映射对象
iterable二元组组成的可迭代对象

返回值

新字典

实例

print(dict(name="小明", age=18))
print(dict([("a", 1), ("b", 2)]))
print(dict(zip(["x", "y"], [10, 20])))
以上实例输出结果为:
{'name': '小明', 'age': 18}
{'a': 1, 'b': 2}
{'x': 10, 'y': 20}