This article mainly introduces the basic knowledge of the dictionary in Python, is the basic knowledge of Python introduction, need friends can refer to the
Dictionaries are mutable and can store any number of Python objects, including other container types, another container type. The dictionary includes key pairs (called items) and their corresponding values.
Python dictionaries are also referred to as associative arrays or hash tables. The general syntax of the dictionary is as follows:
?
1 |
Dict = {' Alice ': ' 2341 ', ' Beth ': ' 9102 ', ' Cecil ': ' 3258 '} |
You can create a dictionary in the following ways:
?
1 2 |
Dict1 = {' abc ': 456}; Dict2 = {' abc ': 123, 98.6:37}; |
Each key comes from its value with a colon (:), the item is separated by commas, and the whole thing is enclosed in curly braces. No item an empty dictionary is written with only two curly braces, just like this: {}
The key is unique in a dictionary, and the value may not be. The value of a dictionary can be of any type, but the key must be an immutable data type, such as a string, a number, or a tuple.
To access the value of a dictionary:
To access the dictionary element, you can use the familiar square brackets together to get the key to its value. The following is a simple example:
?
1 2 3 4 5 6 |
#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A '; print "dict[' name ']:", dict[' name '; Print "dict[' age ']:", dict[' age '; |
When executed in the above code, the following results are produced:
?
1 2 |
dict[' Name ': Zara dict[' age ': 7 |
If you want to access a nonexistent key, this will get an error, as follows:
?
1 2 3 4 5 |
#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A '; Print "dict[' Alice ']:", dict[' Alice ']; |
When the above code is executed, the following results are produced:
?
1 2 3 4 5 |
dict[' Zara ']: traceback (most recent call last): File "test.py", line 4, in <module> print "dict[' Alice '": ", dict[' Alice ']; Keyerror: ' Alice ' |
Update Dictionary:
You can modify an existing entry or delete it by adding a new entry or item (that is, a key-value pair). As a simple example, the following illustration shows an update of the words in an existing entry:
?
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A '; dict[' age ' = 8; # Update existing entry dict[' School ' = ' DPS School '; # ADD new entry print "dict[' Age": ", dict[' age ']; Print "dict[' School ']:", dict[' School ']; |
When the above code is executed, the following results are produced:
?
1 2 |
Dict[' age ']: 8 dict[' School ': DPS School |
To delete a dictionary element:
You can delete a single dictionary element or clear the entire contents of the dictionary. You can also delete the entire dictionary in a single operation.
To delete the entire dictionary, just use the DEL statement. The following is a simple example:
?
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A '; Del dict[' Name ']; # Remove entry with key ' Name ' dict.clear (); # Remove all entries in Dict del dict; # Delete entire dictionary print "dict[' Age '": ", dict[' age ']; Print "dict[' School ']:", dict[' School ']; |
This will produce the following results. Note that the exception is thrown because the dictionary does not exist after Del dict:
?
1 2 3 4 5 |
Dict[' age ': traceback (most recent called last): File "test.py", line 8, in <module> print "dict[' Age": ", dict[' age ']; TypeError: ' Type ' object is unsubscriptable |
Note: The Del () method is discussed in a subsequent section.
Properties of the key of the dictionary:
There is no limit to the dictionary value. They can be any Python object, whether it be a standard object or a user-defined object. But as a key, it is not possible to do so.
To keep in mind the two key points of the dictionary:
(a) A key is not allowed to correspond to multiple entries. This means that you cannot have duplicate keys. When there are duplicate keys, the allocation process is final. The following is a simple example:
?
1 2 3 4 5 |
#!/usr/bin/python dict = {' name ': ' Zara ', ' age ': 7, ' name ': ' Manni '}; print "dict[' name ']:", dict[' name '; |
When the above code is executed, the following results are produced:
?
(b) The value word of the key must be immutable. This means that a string, a number, or a tuple can be used as a key to the dictionary, but it is not allowed to be like [' key ']. The following is a simple example:
?
1 2 3 4 5 |
#!/usr/bin/python dict = {[' Name ']: ' Zara ', ' Age ': 7}; print "dict[' name ']:", dict[' name '; |
When the above code is executed, the following results are produced:
?
1 2 3 4 |
Traceback (most recent): File "test.py", line 3, in <module> dict = {[' Name ']: ' Zara ', ' Age ': 7}; Typeerror:list objects are unhashable |