Python Data Structures-dictionaries

Source: Internet
Author: User

Original address: Http://docs.pythontab.com/python/python3.4/datastructures.html#tut-tuples

The best way to understand a dictionary is to think of it as an unordered key: a set of value pairs (Key:value pairs) whose keys must be distinct (within the same dictionary).

A pair of curly braces creates an empty dictionary: {} .

>>> Tel = {'Jack': 4098,'Sape': 4139}>>> tel['Guido'] = 4127>>>tel{'Sape': 4139,'Guido': 4127,'Jack': 4098}>>> tel['Jack']4098>>>deltel['Sape']>>> tel['Irv'] = 4127>>>tel{'Guido': 4127,'Irv': 4127,'Jack': 4098}>>>list (Tel.keys ()) ['Irv','Guido','Jack']>>>Sorted (Tel.keys ()) ['Guido','Irv','Jack']>>>'Guido' inchteltrue>>>'Jack'  not inchTelfalse

The dict () constructor can create a dictionary directly from the Key-value pair:

>>> dict ('sape', 4139), ('Guido', 4127), ('  Jack', 4098)] {'sape'Jack  'Guido': 4127}

In addition, the dictionary derivation can create a dictionary from any of the key-value expressions:

 for inch (2, 4, 6)} {2:4, 4:16, 6:36}

If the keyword is a simple string, it is sometimes more convenient to specify Key-value by keyword arguments:

>>> dict (sape=4139, guido=4127, jack=4098) {'sape'Jack  'Guido': 4127}

Python Data Structures-dictionaries

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.