dict() is the built-in constructor that creates a Python dictionary, a mutable collection of unique keys mapped to values. You can call it with no arguments for an empty dictionary, with keyword arguments, with an iterable of key-value pairs, or with another mapping to make a shallow copy. Since Python 3.7, dictionaries remember insertion order, so they are no longer "unordered".
For a literal dictionary, {} and {"a": 1} are shorter and slightly faster. Use dict() when the data comes from keyword arguments, zip(), a list of pairs or another mapping.
| Call | Result |
|---|---|
dict() | {} |
dict(name="Alice", age=25) | {'name': 'Alice', 'age': 25} |
dict([("a", 1), ("b", 2)]) | {'a': 1, 'b': 2} |
dict(zip(keys, values)) | Pairs built from two sequences |
dict(other) | A shallow copy of other |
dict.fromkeys(keys, 0) | Every key set to the same value |
Keyword arguments only work when keys are valid Python identifiers, so dict(first-name="A") is a syntax error. Use a list of pairs or a literal for such keys.
zip() pairs items positionally, and dict() turns the pairs into a mapping.
If the lists have different lengths, zip() stops at the shorter one. In Python 3.10+ you can write zip(keys, values, strict=True) to raise an error instead of silently dropping data.
dict(other) creates a new dictionary with the same keys and values. The copy is shallow, so nested lists or dictionaries are shared with the original. Use copy.deepcopy() when you need independent nested data.
For merging, modern Python offers the | and |= operators (Python 3.9+), which are clearer than the older dict(a, **b) trick.
When keys overlap, the right-hand value wins. The dict(a, **b) form only works when all keys in b are strings, so prefer | or {**a, **b}.
A dictionary comprehension is often better than calling dict() on a generator.
Do not pass a mutable value such as [] to fromkeys(). Every key would share the same list. Use a comprehension to create a separate list per key.
Keys must be hashable and unique. Strings, numbers, tuples of hashable items and frozenset work. Lists and dictionaries do not.
Lookups, inserts and deletes take average constant time. Use get() or collections.defaultdict to avoid KeyError on missing keys.
Real data is often nested, and a missing key raises KeyError. Combine get() with defaults, or use defaultdict when you group items.
Converting the defaultdict back with dict() gives a plain dictionary that no longer creates keys on lookup. This is a common reason to call dict() on an existing mapping.
They preserve insertion order, guaranteed by the language since Python 3.7. Equality comparison between dictionaries ignores order.
Both create dictionaries. The literal {} is faster and more readable for fixed data, while dict() accepts keyword arguments, iterables and mappings.
No, it makes a shallow copy. Nested objects are shared, so use copy.deepcopy() for a fully independent copy.
Use a | b in Python 3.9+ or {**a, **b} in older versions. Values from the right-hand dictionary override duplicates.
dict() creates dictionaries from keyword arguments, pairs, zip() results or other mappings. Remember that dictionaries preserve insertion order, copies are shallow, and keys must be hashable. Use | to merge, comprehensions to transform, and literals when the contents are fixed.