Python3 dictionary

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

Each key value of the dictionary (key=>value) is split with a colon (:), separated by a comma (, ). The entire dictionary is enclosed in curly braces ({}) in the following format:

d = {key1 : value1< /span>, key2 : Value2 }

key must be unique, but the value is not necessary.


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'< Span class="pun">, '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 in square brackets, as in the following example:


#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} print ("dict['Name']: ", dict['Name']) print ("dict['Age']: ", dict['Age'])

The output of the above example:

dict['Name' ]: Runoob
Dict['Age']: 7

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


#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}; print ("dict['Alice']: ", dict['Alice'])

The output of the above example:

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

dict['Age'] = 8; # Update Age dict['School'] = "Rookie tutorial" # add information print ("dict['Age']: ", dict['Age']) print ("dict['School']: ", dict['School'])
The above example output:
dict['Age']: 8
dict['School']: Rookie tutorial

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:


#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'} del dict['Name'] # Delete key 'Name' dict.clear() # Clear dictionary del dict # Delete dictionary print ("dict['Age']: ", dict['Age']) print ("dict['School']: ", dict['School'])

This will raise an exception because the dictionary no longer exists after performing the del operation:

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

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

Characteristics of dictionary keys

The dictionary value can be any python object, either a standard object or 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:


#!/usr/bin/python3 dict = {'Name': 'Runoob', 'Age': 7, 'Name': 'Little rookie'} print ("dict['Name']: ", dict['Name'])

The output of the above example:

dict['Name' ]: small rookie

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


#!/usr/bin/python3 dict = {[ 'Name']: ' Runoob', ' Age': 7} print ("dict['Name']: "< Span class="hl-code">, dict['Name'] )

The output of the above example:

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

Dictionary built-in function & method

The Python dictionary contains the following built-in functions:

serial numberfunction and descriptioninstance
Calculate the number of dictionary elements, which is the total number of keys。
>>> dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
>>> len(dict)
Output dictionary, represented by a printable string。
>>> dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
>>> str(dict)
"{'Name': 'Runoob', 'Class': 'First', 'Age': 7}"
Returns the input variable type, if the variable is a dictionary, returns the dictionary type。
>>> dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
>>> type(dict)
<class 'dict'>

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