Python Function Reference

dict

Create a dictionary from another dictionary, a sequence of pairs or keyword arguments.

Python 3.13
dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)
mapping
A dictionary whose pairs are copied.
MappingOptional
iterable
(key, value) pairs, such as a list of tuples.
IterableOptional
**kwargs
Pairs as keyword arguments: the argument name becomes a string key.
Any

Return value

A new dictionary.

Examples

Dictionary from keyword arguments

Python 3.13
print(dict(a=1, b=2))
{'a': 1, 'b': 2}

Dictionary from a list of pairs

Python 3.13
print(dict([('a', 1), ('b', 2)]))
{'a': 1, 'b': 2}

Details

If a key occurs several times, the last value wins. **kwargs can only set string keys that are valid variable names.

See also

Where to learn