Operation of the Python dictionary

Source: Internet
Author: User
Tags shallow copy

One, the operation of the dictionary
a={'name':'wo',' Age': 18}dict.clear ()#Delete all elements in a dictionarya.clear () dict.copy ( )#returns a shallow copy of a dictionarya.copy () dict.get (key, default=none)#returns the value of the specified key if the value does not return the default value in the dictionaryA.get ('h','123') Dict.items ()#returns an array of traversed (key, value) tuples as a listPrint(A.items ())#ResultsDict_items ([('name','wo'), (' Age', 18)]) Dict.keys ()#returns a dictionary of all keys in a listPrint(A.keys ())#ResultsDict_keys ([' Age','name']) dict.values ()#returns all values in the dictionary as a listPrint(A.values ())#ResultsDict_values ([18,'wo']) Dict.fromkeys (seq[, Val] ))#Create a new dictionary with the keys to the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionaryPrint([Dict.fromkeys],'Test'))#Results{1:'Test', 2:'Test', 3:'Test'}dict.setdefault (key, default=none)#similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to defaultA.setdefault ('s','123')Print(a)#Results{'s':'123','name':'wo',' Age': 18}dict.pop (Key[,default])#deletes the value of the dictionary given key key, and returns the value to be deleted. The key value must be given. Otherwise, the default value is returned. Print(A.pop ('123','s'))Print(a)#Resultss{' Age': 18,'name':'wo'}dict.popitem ()#randomly returns and deletes a pair of keys and values in a dictionaryPrint(A.popitem ())Print(a)#Results(' Age', 18){'name':'wo'}dict.update (Kwargs)#give the dictionary a new value, or you can pass in another dictionary, two dictionaries mergedA.update (sex='Mans')Print(a)#Results{'Sex':'Mans','name':'wo',' Age': 18}
Second, the traversal of the dictionary
a={'name':'wo','age':   for in A.keys ()# traverse keyfor in A.values ( )  # traverse the values for in A.items ()# Traversal dictionary
Third, the existence or not judge
a={'name':'wo',' Age': 18}'name' inchA#determine if key is in the dictionary'name' inchA.keys ()#judging = whether in key'name' inchA.values ()#Judging whether in the values

Operation of the Python 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.