Python Dictionary

The dictionary is another variable container model and can store any type of object.

Each key value for the dictionary key=>value is split with a colon :, each key-value pair Intersected with a comma ,, the entire dictionary is enclosed in curly braces {} in the following format:

d = {key1 : value1, key2 : value2 }
The

key is generally unique. If the last key-value pair is repeated, the value does not need to be unique.

>>>dict = {'a': 1, 'b': 2, 'b': '3'} >>> dict[ 'b'] '3'< Span class="hl-code"> >>> dict {'a' : 1, 'b' : '3' }
The

value can take any data type, but the keys must be immutable, such as strings, numbers, or tuples.

A simple dictionary example:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

You can also create a dictionary like this:

dict1 = { 'abc': 456 } dict2 = { ' abc': 123, 98.6 : 37 }

Access the value in the dictionary

Put the corresponding key into the familiar square bracket, as in the following example:

Instance

#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Name']: ", dict['Name'] print "dict['Age']: ", dict['Age']

The above example output:

dict['Name']: Zara
dict['Age']: 7

If you access the data with a key that is not in the dictionary, the error will be output as follows:

Instance

#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Alice']: ", dict['Alice']

The above example output:

dict['Alice']:
Traceback (most recent call last):
File "test.py", line 5, in <module>
print "dict['Alice']: ", dict['Alice']
KeyError: 'Alice'


Modify dictionary

Adding new content to the dictionary is by adding new key/value pairs, modifying or deleting existing key/value pairs as follows:

Instance

#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8 # Update dict['School'] = "RUNOOB" # Add to print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']
The above example output:
dict['Age']: 8
dict['School']: RUNOOB


Delete dictionary elements

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

Show delete a dictionary with the del command, as in the following example:

Instance

#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} del dict['Name'] # The delete key is an entry for 'Name' dict.clear() # Clear all entries in the dictionary del dict # Delete dictionary print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']

But this will cause an exception, because the dictionary no longer exists after using del:

dict['Age']:
Traceback (most recent call last):
File "test.py", line 8, in <module>
print "dict['Age']: ", dict['Age']
TypeError: 'type' object is unsubscriptable

Note:del()The method will also be discussed later.。


Characteristics of dictionary keys

The 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 remember:

1) The same key is not allowed to appear twice. If the same key is assigned twice when creating, the latter value will be remembered, as in the following example:

Instance

#!/usr/bin/python dict = {' Name': 'Zara' , 'Age' : 7, 'Name' : 'Manni' } print "dict['Name']: ", < Span class="hl-identifier">dict['Name']

The output of the above example:

dict['Name' ]: Manni

2) The key must be immutable, so it can be played with numbers, strings or tuples, so using the list will not work, as in the following example:

Instance

#!/usr/bin/python dict = {[ 'Name']: ' Zara', ' Age': 7} print "dict['Name']: ", < Span class="hl-identifier">dict['Name']

The above example output:

Traceback (most recent call last):
File "test.py", line 3, in <module>
dict = {['Name']: 'Zara', 'Age': 7}
TypeError: list objects are unhashable







javacodegeeks is optimized for learning.© javacodegeeks
All Right Reserved and you agree to have read and accepted our term and condition.