dict() Dictionary constructor

Last updated: 2026-09-22

dict() Dictionary constructor

dict() creates a dict from keyword args, iterable of pairs, or another mapping.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

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

⚙️ Parameters

kwargs OptionalKeyword arguments turned into key-value pairs.
mapping OptionalAnother mapping object, e.g. dict or Counter.
iterable OptionalAn iterable of key-value pairs, e.g. [(k, v)].

Returns:dict. A newly created dictionary.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

{'a': 1, 'b': 2} {'x': 10, 'y': 20}
More case studies coming soon — check back later.

❓ FAQ

QWhat are the ways to create a dictionary?
Adict(a=1), dict([('x', 10)]), and dict({'x': 10}) — plus the literal {'x': 10}; all produce the same result, and the literal is the most concise for everyday use.
QWhy does dict('ab') raise an error?
ABecause dict() requires every element of the iterable to be a key-value pair (an iterable that unpacks into two elements, e.g. [('a', 1)]). Each element of 'ab' is a single character, so it cannot be unpacked.
QIs dict(a=1, a=2) valid?
ANo — keyword arguments cannot be repeated. To express duplicate keys use dict([('a', 1), ('a', 2)]), where the later value overwrites the earlier one.
QWhich is better, dict() or the literal {}?
AThe literal {} is faster and more common; dict() is suited to converting from a mapping object or a sequence of key-value pairs, e.g. dict(zip(keys, values)).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏