Python built-in functions (15) -- dict, python built-in functions dict

Source: Internet
Author: User

Python built-in functions (15) -- dict, python built-in functions dict

English document:

Classdict(** Kwarg)

Classdict(Mapping,** Kwarg)

Classdict(Iterable,** Kwarg)

Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.

If no positional argument is given, an empty dictionary is created. if a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. otherwise, the positional argument must be an iterable object. each item in the iterable must itself be an iterable with exactly two objects. the first object of each item becomes a key in the new dictionary, and the second object the corresponding value. if a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.

If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. if a key being added is already present, the value from the keyword argument replaces the value from the positional argument.

 

Note:

1. constructor of the dictionary class.

2. If no parameter is input, an empty dictionary is returned.

>>> dict(){}

3. You can input a key-value pair to create a dictionary.

>>> dict(a = 1){'a': 1}>>> dict(a = 1,b = 2){'b': 2, 'a': 1}

4. You can input a ing function to create a dictionary.

>>> dict(zip(['a','b'],[1,2])){'b': 2, 'a': 1}

5. You can input an iteratable object to create a dictionary.

>>> dict((('a',1),('b',2))){'b': 2, 'a': 1}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.