SoFunction
Updated on 2024-11-16

Python's dictionary adds elements in several ways.

Code used in this article

book_dict = {"price": 500, "bookName": "Python Design.", "weight": "250g"}

The first way:Use []

book_dict["owner"] = "tyson" 

Description: specify the key in parentheses, assign a value, if the key does not exist, it is to add the element (if the key already exists, it is to modify the value corresponding to the key)

The second way:Use the update() method with a dictionary object as argument

book_dict.update({"country": "china"}) 

Description: Use dict's update() method to pass in a new dict object with a non-existing key! (If the key in this new dict object already exists in the current dictionary object, it will overwrite the value corresponding to the key.)

The third way:Use the update() method with keyword arguments

book_dict.update(temp = "Speechless.", help = "Help.") 

Description: the same use of dict's update method, but passed the keyword parameter, the key does not exist is to add elements (key exists is to modify the value)

Note: the keyword parameter form, the key object can only be a string object

Fourth way:Use the update() method with a dictionary unpacking method as argument

my_temp_dict = {"name": "Mr. Wang.", "age":18}
book_dict.update(**my_temp_dict) 

tantamount

book_dict.update(name="Mr. Wang.",age=18) 

Note that dictionaries are completely unordered sets of mappings

1, the dictionary is unordered: when you traverse the dictionary elements, the order in which you add elements, and the order in which you access the elements are not related!

2, when you traverse a dictionary object, if you add elements in the same order as you, this is just a coincidence, the need for element-ordered dictionary see OrderedDict

Deleting Dictionary Elements

Method 1: del function

# Delete method 1: using the del function
del[aa['adress']]
print(aa) # {'talent': 60, 'english': 'english', 'price': 100, 'hhh': 'gogogo'}

Method 2: pop function

# Delete method 2: use the pop function and return the value
vv = ('Talent')
print(vv) # 60
print(aa) # {'English': 'english', 'price': 100, 'hhh': 'gogogo'}

Method 3: clear function

# clear method, removes all
()
print(aa) # {}, empty

dict (dictionary) in python:

1. Dictionary is another variable container model, where each key-value pair is separated by a colon (:), each key-value pair is separated by a comma (,), and the whole dictionary is surrounded by curly braces {};

2. Keys in a dictionary are generally unique, and a later key-value pair overwrites an earlier one if duplicated, although dictionary values need not be unique;

3. The value can take any data type, but the key must be of an immutable type, such as a string, a number or a tuple, but not a list because lists are mutable.

to this article on the Python dictionary to add elements of several ways to this article, more related Python dictionary to add elements of content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!