This article mainly introduces the basic knowledge of the dictionary in Python. It is the basic knowledge in the Python getting started. If you need it, you can refer to the dictionary as variable, you can also store any number of Python objects, including other container types and other container types. The dictionary includes key pairs (called projects) and their corresponding values.
The Python dictionary is also called an associated array or hash table. The general syntax of the dictionary is as follows:
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
You can use the following method to create a dictionary:
dict1 = { 'abc': 456 };dict2 = { 'abc': 123, 98.6: 37 };
Each key comes from its value. It is separated by a colon (:). The entire item is included in braces. No project. An empty dictionary is written with only two braces, like this :{}
The key is unique in a dictionary, but the value may not. Dictionary values can be of any type, but keys must be of an unchangeable data type, such as strings, numbers, or tuples.
Value of the access dictionary:
To access the dictionary element, you can use the key together with the familiar square brackets to obtain its value. The following is a simple example:
#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};print "dict['Name']: ", dict['Name'];print "dict['Age']: ", dict['Age'];
When the above code is executed, the following results are generated:
dict['Name']: Zaradict['Age']: 7
If you want to access a key that does not exist, this will get an error, as shown below:
#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};print "dict['Alice']: ", dict['Alice'];
When the above code is executed, the following results are generated:
dict['Zara']:Traceback (most recent call last): File "test.py", line 4, in
print "dict['Alice']: ", dict['Alice'];KeyError: 'Alice'
Update dictionary:
You can add a new entry or project (that is, a key-Value Pair) to modify or delete an existing entry. As a simple example, update a word in an existing entry:
#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};dict['Age'] = 8; # update existing entrydict['School'] = "DPS School"; # Add new entryprint "dict['Age']: ", dict['Age'];print "dict['School']: ", dict['School'];
When the above code is executed, the following results are generated:
dict['Age']: 8dict['School']: DPS School
Delete dictionary elements:
You can delete a single dictionary element or clear all contents in the dictionary. You can also delete the entire dictionary in a single operation.
To delete the entire dictionary, you only need to use the del statement. The following is a simple example:
#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};del dict['Name']; # remove entry with key 'Name'dict.clear(); # remove all entries in dictdel dict ; # delete entire dictionaryprint "dict['Age']: ", dict['Age'];print "dict['School']: ", dict['School'];
This will produce the following results. Note that an exception is thrown because the dictionary does not exist after del dict is deleted:
dict['Age']:Traceback (most recent call last): File "test.py", line 8, in
print "dict['Age']: ", dict['Age'];TypeError: 'type' object is unsubscriptable
Note: The del () method will be discussed in subsequent sections.
Attributes of the dictionary key:
The dictionary value is unlimited. They can be any Python objects, either standard objects or user-defined objects. But as a key, this is not allowed.
Remember the two key points in the dictionary:
(1) One key cannot correspond to multiple entries. This means that duplicate keys are not allowed. When duplicate keys exist, the last allocation prevails during the allocation process. The following is a simple example:
#!/usr/bin/pythondict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};print "dict['Name']: ", dict['Name'];
When the above code is executed, the following results are generated:
dict['Name']: Manni
(2) The key value must be unchangeable. This means that strings, numbers, or tuples can be used as Dictionary keys, but ['key'] is not allowed. The following is a simple example:
#!/usr/bin/pythondict = {['Name']: 'Zara', 'Age': 7};print "dict['Name']: ", dict['Name'];
When the above code is executed, the following results are generated:
Traceback (most recent call last): File "test.py", line 3, in
dict = {['Name']: 'Zara', 'Age': 7};TypeError: list objects are unhashable