A Dictionary of Python

Source: Internet
Author: User

Dictionary (dictionary): dictionary definition using {}, curly braces, each value separated by ",", Key and value using ":".

Dict1 = {'Tom'amercia'Jerry' ' Amercia '] ,'jack'England']}

Features of the dictionary:
The dictionary is unordered because it has no subscript and is indexed with key, so it is unordered
The key of the dictionary must be unique because it is indexed by key, so key cannot be duplicated and is inherently heavy.

Some operations of the dictionary

add element

1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3dict1['Rose'] = [21,'England']

modifying elements

Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}dict1['Tom'] = [14,'Amercia']

Delete Element

1 Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 Dict1.pop ('Jerry')#Delete the specified element3 delDict1 ('Jerry')#use the Del method to delete

Querying elements

1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3 Print(Dict1.get ('Jack'))#this way, if key does not exist, it will return none.4 5 Print(dict['Jack'])#this way, if key does not exist, it will be an error .6 7 Print('Jack' inchDICT1)#determine if Jack is in this dictionary, returns TRUE or false

Built-in methods

1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3 Print(Dict1.keys ())#Print all keys4 5 Print(Dict1.values ())#Print all value6 7 Print(Dict1.setdefault. ('Judy', [20,'England']))#If key exists, does not move, does not exist, add8 9Dict2 = {'John': [24,'Russia']}Ten  OneDict1.update (DICT2)#Update dictionary, if key exists, update; not present, add

Print (Dict1.items ()) #转变成列表

A Dictionary of Python

Related Article

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.