Skip to main content

dict()

The dict() function in Python returns a new dictionary object. It can be used to create a dictionary by specifying key-value pairs or by passing another dictionary object or an iterable of key-value pairs.

Parameter Values

Parameter Description
mapping

An optional mapping (e.g., a dictionary) to initialize the key-value pairs.

kwargs

Optional keyword arguments. Each keyword argument is treated as a key-value pair for the dictionary.

Return Values

The dict() function returns a dictionary object which can hold various key-value pairs.

How to Use dict() in Python

Example 1:

The dict() constructor builds dictionaries directly from sequences of key-value pairs.

dict([('a', 1), ('b', 2), ('c', 3)])
Example 2:

It can also be used to create dictionaries with keyword arguments.

dict(one=1, two=2, three=3)
Example 3:

Another way to create a dictionary is by passing a dictionary's copy.

dict({'one': 1, 'two': 2, 'three': 3})