Python3 dictionary

Python3 dictionary

Dictionary is another variable container model, and can store any type of object.
A dictionary is a variable container that stores data in key/value pairs. The so-called variable means that the size of the container can be changed, and the elements can be modified or deleted. If you are familiar with Java, you will know that python's dictionary is similar. In Java's hashtable collection, each key-value (key=>value) pair of the dictionary is separated by a colon (:), and each pair is separated by a comma (, ). The entire dictionary is enclosed in braces {}, and JSON The format of is a bit similar, the declaration format is as follows:

d = {key1 : value1, key2 : value2 }

In the same dictionary, the key must be unique, but the value need not.
The value can take any data type, but the key must be immutable, such as a string, number, or tuple.
Example of declaration dictionary code:

dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}
dict2 ={"age":20,"hight":170}
dict3 ={1:12.5,2:20,2.5:"test","one":12.23}  #Different types can be used

print(dict1)print(dict2)print(dict3)

operation result:

{'name':'Zhang San','age': '20','address':'Hunan'}
 {‘age’: 20, ‘hight’: 170}
 {1: 12.5, 2: 20, 2.5: ‘test’, ‘one’: 12.23}

Access the value in the dictionary

Use square brackets to access the value in the dictionary, put the key in the square brackets, you can get the value corresponding to the key, code example:

dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}
dict2 ={"age":20,"hight":170}
dict3 ={1:12.5,2:20,2.5:"test","one":12.23}  #Different types can be used

print(dict1["name"])print(dict2["hight"])print(dict3[1])

operation result:

Zhang San
 170
 12.5

**If you access a key that does not exist in the dictionary, an exception will be thrown, error example: **

dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}print(dict1["sex"])

operation result:

Traceback (most recent call last):
   File “E:/PythonProject/TestDict1.py”, line 3, in  
     print(dict1[“sex”])
 KeyError: ‘sex’

Modify dictionary

To add a new element to the dictionary, you only need to add a new key/value pair. To modify an existing value, you need to re-assign the value through the key. Code example:

dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}

dict1["name"]="Li Si"print("Modify the value of the name key:", dict1)

dict1["sex"]="male"print("Added a key value:", dict1)

operation result:

Modified the value of the name key: {'name':'李四','age': '20','address':'Hunan'}
Added a key value: {'name':'李四','age': '20','address':'Hunan','sex':'Male'}

Delete dictionary element

A single element can be deleted and all elements in the entire dictionary can be cleared. To clear it, you only need to call the clear() method.
Delete the dictionary object and delete a key value in the dictionary using the del command, code example:

dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}

del dict1["name"]  #Delete the name key value in the dictionary
del dict1  #Delete the entire dictionary object
dict1.clear()  #Clear the elements in the dictionary, the dictionary object will not be deleted

Characteristics of dictionary keys

The dictionary value can be any python object, either a standard object or a user-defined one, but the key does not work.
Two important points need to be remembered:
1 ) The same key is not allowed to appear twice. If the same key is assigned two or more times during creation, the value of the last key shall prevail. Code example:

dict1 ={"name":"Zhang San","age":"20","address":"Hunan","name":"Li Si"}print(dict1["name"])print(dict1)

operation result:

Li Si
{'name':'Li Si','age': '20','address':'Hunan'}

2 ) The key must be immutable, so it can be used as a number, string or tuple, but a list will not work. Examples of errors:

dict1 ={["name"]:"Zhang San","age":"20","address":"Hunan"}print(dict1)

operation result:

Traceback (most recent call last):
   File “E:/PythonProject/TestDict2.py”, line 1, in  
dict1 = {["name"]: "Zhang San", "age": "20", "address": "Hunan"}
 TypeError: unhashable type: ‘list’

Dictionary built-in functions & methods

Recommended Posts

Python3 dictionary
Python implements electronic dictionary
Python CookBook
Python FAQ
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Detailed usage of dictionary in Python
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
How to sort a dictionary in python
Centos6 install Python2.7.13
Python answers questions
Python exit loop
Ubuntu16 upgrade Python3
Centos7 install Python 3.6.
ubuntu18.04 install python2
Python classic algorithm
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python string manipulation
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python3 basic syntax
Python review one
linux+ubuntu solve python
Functions in python
Python learning-variable types
CentOS install Python 3.6
7 features of Python3.9
Python file operation