[learn Python happily] - Python dictionary dict

Posted by billy2shoe on Fri, 21 Jan 2022 07:46:56 +0100

1, Dictionary dict description in Python

The dictionary in Python is different from string / list / tuple, because the dictionary dict needs to be composed of two parts: key and value, which are referred to as key value pairs for short. The characteristics of the dictionary are explained in detail below:

1. It is composed of one or more key value pairs. The types of key value pairs can be different or the same;

2. The key value pairs in the dictionary need to be written inside the {} brackets. The key and value in the key value pairs are separated by: and the key value pairs are separated by commas;

3. Dictionary is an unordered set;

4. The key in the dictionary is unique;

Key Value pair: {key: Value}

Dictionary: {age: 18}

Dictionary: {name ":" Zhang San "}

 

2, Definition of dictionary dict in Python

dict1 = dict() # Define an empty dictionary
print(dict1)
print(type(dict1)) # Output dictionary type dict
print(len(dict1)) # Gets the number of dictionary key value pairs


print("***"*20) # Tip: print 60 directly*
#A dictionary can consist of one or more key value pairs separated by commas
# "name" / "age" / "sing_gou" is the key
# "Lao Wang next door" / 42 / True is value
dict2 = {"name":"Lao Wang next door", "age":42, "singe_dog":True} 
print(dict2)
print(type(dict2)) # Output dictionary type dict
print(len(dict2)) # Gets the number of dictionary key value pairs

print("***"*20) # Tip: print 60 directly*
# Two identical keys appear in the dictionary: "age". By default, the following value will overwrite the previous value
dict3 = {"name":"Lao Wang next door", "age":42, "singe_dog":True,"age":15}
print(dict3)
The operation result shows:
{}
<class 'dict'>
0
************************************************************
{'name': 'Lao Wang next door', 'age': 42, 'singe_dog': True}
<class 'dict'>
3
************************************************************
{'name': 'Lao Wang next door', 'age': 15, 'singe_dog': True}

Process finished with exit code 0

1. Adding data to Python dictionary dict

If you want to add data to the dictionary, you can assign a value directly, which is relatively simple

dict1["name"] = "Ape theory python" # Add the key value pair "name": "python" to dict1
dict1["url"] = "shuopython.com" # Add key value pair "url":"shuopython.com" to dict1

print(len(dict1)) # Gets the number of dictionary key value pairs
print(dict1) # Output entire dictionary

The operation result shows:
{}
2
{'name': 'Ape theory python', 'url': 'shuopython.com'}

2. Python dictionary dict delete data

Dictionaries are out of order, and each key value pair has no index value, which is also the difference between dictionaries and Strings / lists / primitives;

To delete the data in the dictionary, you need to delete the corresponding key value pair according to the key value in the key value pair, and the del keyword is used for deletion;

dict1 = {"name":"zhangsan","age":38,"sing_dog":True} # Define a dictionary
print(dict1) # Output dictionary before deletion

# Delete the key value pair corresponding to the key equal to "name"
del dict1["name"]
print(dict1)

# Delete the key value pair corresponding to the key equal to "age"
del dict1["age"]
print(dict1)

# Delete the key value pair corresponding to "sing_dog"
del dict1["sing_dog"]
print(dict1)
The operation result shows:
{'name': 'zhangsan', 'age': 38, 'sing_dog': True}
{'age': 38, 'sing_dog': True}
{'sing_dog': True}
{}

3. Python dictionary dict modify data

Dictionaries are out of order, and each key value pair has no index value, which is also the difference between dictionaries and Strings / lists / primitives;

Modifying the key value pair data in the dictionary is the same as deleting data. You can operate directly according to the key value in the key value pair;

dict1 = {"name":"zhangsan","age":38,"sing_dog":True} # Define a dictionary
print(dict1) # Output dictionary

# Modify the key to be equal to the value corresponding to "name"
dict1["name"] = "Lao Wang next door"
print(dict1) # Output dictionary

# Modify the key to be equal to the value corresponding to "age"
dict1["age"] = 18
print(dict1) # Output dictionary

# Modify the key to be equal to the value corresponding to "sing_dog"
dict1["sing_dog"] = False
print(dict1) # Output dictionary

The operation result shows:
{'name': 'zhangsan', 'age': 38, 'sing_dog': True}
{'name': 'Lao Wang next door', 'age': 38, 'sing_dog': True}
{'name': 'Lao Wang next door', 'age': 18, 'sing_dog': True}
{'name': 'Lao Wang next door', 'age': 18, 'sing_dog': False}

3. Python dictionary dict query data

Dictionaries are out of order, and each key value pair has no index value, which is also the difference between dictionaries and Strings / lists / primitives;

Querying the key value pair data in the dictionary is the same as deleting / modifying data. You can directly operate according to the key value in the key value pair;

3, Python dictionary dict common functions

1.update function - add one dictionary to the end of another dictionary and merge the two dictionaries into one dictionary;

2.clear function - clear the dictionary;

3. dict traversal of Python dictionary

a. use dict.items to traverse the dictionary

The return value of dict.items method is a tuple, which is equivalent to the epoch group tuple

b. traverse the dictionary according to the key or value in the dictionary

dict1 = {"name":"zhangsan","age":38,"sing_dog":True,"height":"155cm"} # Define a dictionary
print(dict1) # Output dictionary

print("***"*20) # Tip: promise 60 directly*
# Method 1: traverse according to the key value
for key in dict1.keys():
    print(key ,dict1[key])  # dict1[key] value corresponding to key in equivalent key value pair

print("***"*20) # Tip: promise 60 directly*
# Method 2: traverse according to the value
for value in dict1.values():
    print(value )

The operation result shows:
{'name': 'zhangsan', 'age': 38, 'sing_dog': True, 'height': '155cm'}
************************************************************
name zhangsan
age 38
sing_dog True
height 155cm
************************************************************
zhangsan
38
True
155cm

Topics: Python Testing dict