dict() Dictionary constructor
Last updated: 2026-09-22
dict() Dictionary constructor
dict() creates a dict from keyword args, iterable of pairs, or another mapping.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
dict(**kwargs)
dict(mapping)
dict(iterable)
⚙️ Parameters
| kwargs Optional | Keyword arguments turned into key-value pairs. |
|---|---|
| mapping Optional | Another mapping object, e.g. dict or Counter. |
| iterable Optional | An iterable of key-value pairs, e.g. [(k, v)]. |
Returns:dict. A newly created dictionary.
💥 Raises
TypeError— Raised when multiple positional arguments or invalid keywords are given.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
{'a': 1, 'b': 2} {'x': 10, 'y': 20}
🏷️ Related Built-in Functions
💡 Related Cases
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)).