Python's Dict Dictionary

Source: Internet
Author: User

One, Dict (Dictionary) type

Dict (Chinese dictionary) is another variable container model and can store any type of object.

Each key value of the dictionary (key=>value) pairs with a colon (:) split, each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces ({}), as shown in the following format:

 1  #  -*- Coding:utf-8-*- 2  3  D = { 4  "  adam  " : 95,  5   " lisa  Span style= "COLOR: #800000" > ": 85,  6   " bart   ' : 597 } 

We call the name key, and the corresponding result, called Value,dict, is to find value by key.

The curly braces {} indicate that this is a dict, and then follow the Key:value and write them out. The last comma of a key:value can be omitted.

The key must be unique, but the value does not have to be.

The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.

1. Visit dict

Place the corresponding key into the familiar square brackets, as in the following example (you can also use the Get method provided by the dictionary: Dict.get (' Bart '))

1 #-*-coding:utf-8-*-2 3Dict = {4     'Adam': 95,5     'Lisa': 85,6     'Bob': 597 }8 9 Print 'Adam =', dict['Adam']Ten Print 'Lisa =', dict['Lisa'] One Print 'Bob =', dict['Bob']

2, modify the Dict

The way to add new content to a dictionary is to add a new key/value pair, modify or delete an existing key/value pair as follows:

1 #-*-coding:utf-8-*-2 3Dict = {4     'Adam': 95,5     'Lisa': 85,6     'Bob': 597 }8 9dict['Adam'] = 100#Modify the value of ' Adam ' in the dictionaryTendict['Paul'] = 66#add a new key-value pair One PrintDict

3. Remove elements from Dict

1 #-*-coding:utf-8-*-2 3Dict = {4     'Adam': 95,5     'Lisa': 85,6     'Bob': 59,7     'Paul': 758 }9 Ten deldict['Adam'];#Delete key is an entry for ' Adam ' OneDict.clear ();#Empty Dictionary A delDict#Delete Dictionary

After you delete a dictionary, the dictionary does not exist, which means that you can no longer access the dictionary.

4. Characteristics of dictionaries

(1) Fast search speed

The search speed is the same regardless of whether the dict has 10 elements or 100,000 elements. The search speed of the list decreases as the element increases.

But Dict's search speed is not without cost, Dict's disadvantage is that the memory is large, but also waste a lot of content, the list is just the opposite,

The memory footprint is small, but the lookup speed is slow.

(2) The dictionary value can take any Python object without restriction, either as a standard object or as a user-defined one, but not a key.

The same key is not allowed to appear two times.

The key must be immutable, so it can be used as a number, string, or tuple, so you can't use a list.

(3) The second feature of Dict is that the stored key-value sequence pair is not in order! This is not the same as the list.

5, Dictionary built-in method

The Python dictionary contains the following built-in methods:

Reference article: http://www.runoob.com/python/python-dictionary.html

"Introduction to Python" by Mu-class network

 

Python's Dict Dictionary

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.