Python Reference ยท Built-in Functions
Python dict() function
Reference for Python dict(): build a dictionary from keyword arguments, pairs or another mapping, with copy behavior, duplicate keys and common mistakes.
dict() is the constructor of Python's built-in dictionary type. Called with no arguments it returns an empty dictionary. Otherwise it builds a new dictionary from another mapping, from an iterable of key-value pairs, from keyword arguments, or from a combination of these.
Most of the time you write a dictionary with the literal syntax {"a": 1}. The dict() call is useful when the data already exists as pairs (for example from zip()), when you want a quick copy of a mapping, or when the keys are valid identifiers and you prefer keyword arguments.
config = dict(host="localhost", port=8080) # {'host': 'localhost', 'port': 8080}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
dict() dict(mapping, **kwargs) dict(iterable, **kwargs) dict(**kwargs)
| Name | Description |
|---|---|
mapping |
An object with keys() and __getitem__(), such as another dictionary. Its items are copied. |
iterable |
An iterable whose elements are each exactly two items long: a key and a value. |
**kwargs |
Extra keyword arguments. Each name becomes a string key. They are applied after the positional argument. |
Return value: a new dictionary. The positional argument is never modified.
Raises: TypeError if a key is not hashable or if more than one positional argument is given; ValueError if an element of the iterable is not a sequence of length two.
Usage notes
- Shallow copy.
dict(d)copies the keys and values, but the values themselves are not copied. Nested lists and dictionaries are shared between the original and the copy. Usecopy.deepcopy()for independent nested data. - Insertion order is kept. Since Python 3.7 dictionaries remember the order in which keys were first inserted, and
dict()follows the order of its input. - Later wins. If a key appears twice, the last value is kept, and keyword arguments override the positional argument.
- Keyword keys must be identifiers.
dict(first-name="x")is a syntax error, and keys built this way are always strings. For other keys use pairs or a literal. - Pairs can be any 2-item sequence. Lists, tuples and even two-character strings work, which is why
dict(["ab", "cd"])gives{'a': 'b', 'c': 'd'}. - Literals are faster.
{}avoids a function lookup and call, so use it for an empty dictionary unless you needdictas a callable (for example as adefaultdictfactory). - Merging. To combine dictionaries,
a | b(Python 3.9+) or{**a, **b}is usually clearer thandict(a, **b), which also requires string keys inb. - dict is a class. You can subclass it and test with
isinstance(x, dict).