A dictionary of Python data types

Source: Internet
Author: User
Tags delete key

First, what is a dictionary

1. The dictionary is the only type of mapping in Python, consisting of key-value pairs enclosed in {},

2. The key of the element in the dictionary: must be a hash immutable data type that can be used as a key in the dictionary

3. The value of the element in the dictionary: there is no limit to what data type can be

Second, the dictionary additions and deletions to search

1. Increase

Dic[key]=value add directly (Key:value) (this method can also be used to modify the dictionary)

Dic.setdefault (Key,value) If the key is already present in the dictionary, do nothing, otherwise add (Key:value)

Do not enter the value parameter to query, if not the key will return none

2. By deleting

Pop (key) has a return value that returns the value corresponding to key

Del Dic[key] Delete key and his corresponding value (del dic = delete entire dictionary, again query cannot find this dictionary)

Popitem () random Delete, latest Python version delete last key value pair

Clear () Empty dictionary dictionary into empty Dictionary {}

3. Change

Dic[key] = value forced modification

Dic.update (dictionary) The old Dictionary before the new dictionary is overwritten

4. Enquiry

Dic.get (Key)

For loop

Dic[key]

SetDefault (mentioned above)

Third, the special operation of the dictionary

Dic.keys gets all the keys in the dictionary stored in a high-copy list

Dic.values gets all the values in the dictionary stored in a high-copy list

Dic.items gets all the key-value pairs in the dictionary in a high-copy list

Iv. Deconstruction

A, B = (a=1,b=2)

A, B = "a" (a=1,b=2)

V. Nesting of dictionaries

Similar to the list of nesting!

  

A dictionary of Python data types

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.