Python dict()

Different forms of dict() constructors are:

class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)

Note: **kwarg let you take an arbitrary number of keyword arguments.

A keyword argument is an argument preceded by an identifier (eg. name=). Hence, the keyword argument of the form kwarg=value is passed to dict() constructor to create dictionaries.

dict() doesn't return any value (returns None).


Example 1: Create Dictionary Using keyword arguments only

Output

numbers = {'y': 0, 'x': 5}
<class 'dict'>
empty = {}
<class 'dict'>

Example 2: Create Dictionary Using Iterable

Output

numbers1 = {'y': -5, 'x': 5}
numbers2 = {'z': 8, 'y': -5, 'x': 5}
numbers3 = {'z': 3, 'y': 2, 'x': 1}

Example 3: Create Dictionary Using Mapping

Output

numbers1 = {'x': 4, 'y': 5}
numbers2 = {'x': 4, 'y': 5}
numbers3 = {'x': 4, 'z': 8, 'y': 5}

Recommended Reading: Python dictionary and how to work with them.