ScanSkill

dict

This dict() function returns a dictionary object i.e. it converts an object into a python dictionary.

Syntax

dict([**kwargs])
dict([mapping, **kwargs])
dict([iterable, **kwawrgs])

Here,

  • kwargs: Optional. Keyword arguments. **kwargs let you take an arbitrary number of keyword arguments.
  • mapping: Optional. Another dictionary.
  • iterable: Optional. An iterable object, in the form of key-value pair(s). keys are immutable.

Examples

  • With keywords arguments only
>>> dict(a=1, b=2)
{'a': 1, 'b': 2}
  • With iterable
# Without Keyword arguments
>>> dict([('a', 1), ('b', 2)])
{'a': 1, 'b': 2}

>>> # Passing Keyword arguments also
>>> dict([('a', 1), ('b', 2)], c=3)
{'a': 1, 'b': 2, 'c': 3}

>>> # Using iterable, zip() creates an iterable
>>> dict(zip(['a', 'b', 'c'], [1, 2, 3]))
{'a': 1, 'b': 2, 'c': 
  • With mapping
>>> dict({'a': 1, 'b': 2})
{'a': 1, 'b': 2}

>>> # Passing keyword arguments also
>>> dict({'a': 1, 'b': 2}, c=3)
{'a': 1, 'b': 2, 'c': 3}