conversion
dict()
Constructs a dictionary from keyword args, another mapping, or an iterable of key/value pairs. Insertion order is preserved (Python 3.7+).
dict(**kwargs) | dict(mapping, **kwargs) | dict(iterable, **kwargs) Parameters
| Parameter | Purpose |
|---|---|
| mapping | Existing dict-like to copy |
| iterable | Iterable of (key, value) pairs |
| **kwargs | Keyword args become string keys |
Examples
dict(a=1, b=2) Returns {'a': 1, 'b': 2}
dict([('x', 1), ('y', 2)]) Returns {'x': 1, 'y': 2}
dict(zip(['a', 'b'], [1, 2])) Returns {'a': 1, 'b': 2}
Gotcha
Keyword-arg form only allows valid identifier keys. Later keys silently override earlier ones — no duplicate warning.
Related built-ins
zip()
Pairs elements from multiple iterables into tuples, stopping at the shortest. With strict=True, raises ValueError if lengths differ.
str()
Returns a str version of an object. With bytes/bytearray, decodes using encoding.
int()
Constructs an integer from a number or string. When x is a string, base selects the numeric base (0 auto-detects from prefix).
float()
Constructs a floating-point number from a number or string. Accepts 'inf', '-inf', 'nan' (case-insensitive).