Python Dictionary Dict

Source: Internet
Author: User

First, dictionary (dict)1. Dictionaries

Dictionary: denoted by {}, the key value is unique to data {Key:value}.

Key: All must be hashed, immutable data types can be used as key values in the dictionary without any restrictions.

2. Search and delete of dictionaries

1 increase

dic[Key Name] = Value Added, Force add

Dic.setdefault (Key,value) If there is no action in the dictionary, add it if it does not exist.

1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2dic["K4"] ="V4"3Dic.setdefault ("K5","V5")

2. Delete

Dic.pop (key) is deleted by key and the return value is the deleted value

Del Dic[key] deleted via key

Clear () Empty dictionary

Popitem () random deletion (python3.6 is the deletion of the last one, python3.5 is randomly deleted)

1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2Dic.pop ("K1")3 deldic["K2"]4Dic.popitem ()

3. Modifications

Dic[key] = value forced modification

Dic.update (dict) Update/upgrade

1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2Dic1 = {"Q1":"W1","Q2":"W2","Q3":"W3"}3dic["K1"] ="B1"4Dic.update (DIC1)

4. Find

The For loop defaults to getting the keys in the dictionary

Dic[key] View 1: Error When you don't have this key

Dic.get (key, return value) View 2: None returns none, specifying what to return

Dic.setdefault (Key) View 3: No then return none

1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2  forIinchDIC:3     Print. IO4 Print(dic["K1"])5 Print(Dic.get ("K2"))6 Print(Dic.setdefault ("K3"))

3. Other operations of the dictionary:

Keys gets to all the keys that exist in a high-imitated list

Values gets to all the values that exist in a high-imitated list

Items gets to all the key values in the form of a meta-ancestor exist in a highly imitated list

1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2  forIinchDic.keys ():3     Print(i)4  forIinchdic.values ():5     Print(i)6  forIinchDic.items ():7     Print(i)

Deconstruction:

A, B = 1, 2

A, B = (up to)

b = [+]

1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2  forA, binchDic.items ():3     Print(a)4     Print(b)

4. Nesting of dictionaries

DIC = {Key1:value1,key2:value2,{key3:value3,key4:value4}}

1DIC = {'name':'Xiao Ming',' Age': 15,'Family':{2                     'name':' Daming',3                     ' Age': 39,4                     'Salary': 1000005                 },6                 'friend':[7{'name':'Big Bear',' Age': 16},8{'name':'Kumaji',' Age': 14},9                 ]Ten             } One Print(dic['friend'][1]['name'])

Python Dictionary Dict

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.