dict

Full name
dict
Library
Built-in
Syntax

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

Description

The dict function creates a new dictionary. If no arguments are included, an empty dictionary will be created. If an argument is included and this is an object of type "mapping", an empty dictionary will be created with the same key-value pairs as the mapping object. In any other case, the argument must be an iterable object, and each element of the object must, in turn, be an iterable object with exactly two objects. The first object will become a key in the new dictionary, and the second object will become the corresponding value. If a key appears more than once, only the last occurrence will be included in the dictionary.

Examples

If no arguments are included, an empty dictionary is created:

Dict function Example of use

 

If an iterable object is added, it must be made up of iterable objects containing two objects, which will be considered keys and values of the new dictionary:

Dict function Example of use

 

Below are several ways to create the same dictionary from different objects:

Dict function Example of use

 

Submitted by admin on Sun, 01/20/2019 - 11:39