Python Basic Tutorial Note 14: Dictionary (Dictionary)

Source: Internet
Author: User
Tags delete key

Pythondictionary (Dictionary)

A dictionary is another mutable container model and can store any type of object.

Each key value of the dictionary is key=>value with a colon : Split, with a comma, split between each key-value pair, and the entire dictionary is enclosed in curly braces {}, as shown in the following format:

D = {key1:value1, key2:value2}

The key is generally unique, and the value does not need to be unique if the last key value pair is repeated instead of the previous one.

>>>dict = {'a': 1,'b': 2,'b':'3'};>>> dict['b']'3'>>>dict{'a': 1,'b':'3'}

The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.

A simple Dictionary instance:

Dict = {'Alice'2341'Beth ' '9102'Cecil'3258' }

You can also create a dictionary like this:

' ABC ': 456'abc': 123, 98.6:37};
Accessing values in the dictionary

Put the corresponding key into the familiar square brackets, the following example:

Dict = {'Name':'Zara',' Age': 7,'Class':' First'}; Print "dict[' Name ']:", dict['Name'];Print "dict[' age ']:", dict[' Age'];

Results:

dict['Name':  zaradict['age':  7

If the data is accessed using a key that is not in the dictionary, the output error is as follows:

Dict = {'Name''Zara'' age '  Class'First'print ' ", dict['Alice'];  

Results:

dict['Alice': Traceback (most recent call last)  :"test.py  " in <module>    print"", dict['   Alice ' Alice '
Modify Dictionary

The way to add new content to a dictionary is to add a new key/value pair, modify or delete an existing key/value pair as follows:

Dict = {'Name':'Zara',' Age': 7,'Class':' First'}; dict[' Age'] = 8;#Update Existing entrydict['School'] ="DPS School";#ADD new Entry  Print "dict[' age ']:", dict[' Age'];Print "dict[' School ']:", dict['School'];

Results:

dict['age ':  8dict['School'] :  DPS School
Delete a dictionary element

The ability to delete a single element also clears the dictionary, emptying only one operation.

Show Delete a dictionary with the Del command, as in the following example:

Dict = {'Name':'Zara',' Age': 7,'Class':' First'}; deldict['Name'];#Delete key is ' Name ' entryDict.clear ();#Empty dictionary all entriesdelDict;#Delete Dictionary Print "dict[' age ']:", dict[' Age'];Print "dict[' School ']:", dict['School'];

However, this throws an exception because the dictionary no longer exists after Del:

dict['age ']:traceback (most recent call last):  "test.py "  in <module>    print"", dict['age  ' type'is unsubscriptable
Properties of Dictionary Keys

A dictionary value can take any Python object without restriction, either as a standard object or as a user-defined one, but not a key.

Two important points to keep in mind:

1) The same key is not allowed to appear two times. When created, if the same key is assigned a value of two times , the latter value is remembered, as in the following example:

Dict = {'Name''Zara'' age '  Name'manni'print ", dict['Name'];  

Results:

dict['Name']:  Manni

2) The key must be immutable, so it can be used as a number, string or tuple, so the list is not, the following example:

Dict = {['Name''Zara'age' : 7print"", dict['Name' ];

Results:

Traceback (most recent):   " test.py "  in <module>    = {['Name''Zara'  'age': 7}; Typeerror:list Objects is unhashable

Python Basic Tutorial Note 14: Dictionary (Dictionary)

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.