Additions and deletions of dictionaries in 2018.8.1 Python and other operations

Source: Internet
Author: User

A simple introduction to the dictionary

1. Dict with {} to indicate key value pair data {Key:value} uniqueness

2, the key must be a hash, immutable data type can be used as a key in the dictionary

There is no limit to the value

3. Known hash (immutable) data type: Int,str,tuple,bool

Non-hashed (variable) data type: List,dict,set

4. Syntax: {key1:value1,key2:value2}

DIC = {'name':'Alex',' Age': 9000}#string Printdic (DIC)= {1:'a', 2:'b', 3:'C'}#Digital Printdic (DIC)= {True:'1', False:'0'}#Boolean value Printdic (DIC)= {():'ABC'}#Meta-group Printdic (DIC)= {[+]:'ABC'} Print(DIC)

Second, the dictionary additions and deletions and other related operations

1, increase

Dic[key] = value

Dic.setdefault (Key,value) If the key exists in the dictionary without any action, add

Can be queried by key, without this key to return none

====================================================================

2. By deleting

Dic.pop (Key) has a return value that is returned by the deleted value
Dic.del Dic[key]
Dic.popitem () Random deletion
Dic.clear () Empty dictionary

======================================================================

3, change

Dic[key] = value
Dic.update (dictionary)

=======================================================================

4. Check

Dic.get (Key)
Dic[key]
For loop
SetDefault (Key)

DIC = {'Yi Master':'Sword Saint','Jian Hao':'Nursery','Bush LUN':'Big Sword'} forIinchDIC:Print(i)#The for loop defaults to getting the keys in the dictionaryPrint(dic['Yi Master Father'])                   #View 1 The query will error when there is no such key.Print(Dic.get ('Yi Master','You're stupid, no!'))    #See 2 No return none can specify what to returnPrint(Dic.setdefault ('Yi Tai Normal Lake'))        #View 3 no return none

=======================================================================

5. Other related operation

Print(Dic.keys ())#(High copy list)Print(Dic.values ())#(High copy list)Print(Dic.items ())#(High imitation list, tuple in list) forIinchDic.keys ():Print(i)#get to each key in the dictionary forIinchDIC:Print(i)#get to each key in the dictionary forIinchdic.values ():Print(i)#get to every value in a dictionary forKey,valueinchDic.items ():Print(Key,value)#Deconstruction

6, the Dictionary of Nesting

DIC = {    'name':'Wang Feng',    ' Age': 43,    'wife':{        'name':'International Chapter',        ' Age': 39,        'Salary': 100000    },    'Baby':[        {'name':'Big Bear',' Age': 18},        {'name':'Kumaji',' Age': 15},]}dic['Baby'][0][' Age'] = 19Print(DIC)

Additions and deletions of dictionaries in 2018.8.1 Python and other operations

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.