“This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

How do I add, modify, and delete elements in a dictionary

Knowledge depot: In Python programs — keys are unique, but values can be repeated!

Add data to the dictionary:

(1) Base operation not 6!

Here’s what you need to know — in Python, a dictionary is a dynamic structure into which you can add “key/value” pairs at any time.

To add data to a dictionary, specify the name of the dictionary, enclose the key in brackets, and specify the value of the key.

We use this method to add our math and English scores to xiao Hong:

dict = {'line generation': "99"."Data analysis": "99"."Probability theory": "98"}      # create a dictionary

dict['mathematics'] = 100        Add dictionary 1
dict['English'] = 99         # add dictionary 2

print(dict)               Print the dict values
print('Xiao Hong's math score is:'.dict['mathematics'])     # show your math score
Copy the code

Note Note: The order in which the key-value pairs are arranged is different from the order in which they are added. Because Python doesn’t care about the order in which key-value pairs are added, it only cares about the relationship between keys and values!!

② Modify the dictionary:

(1) Base operation not 6!

First specify the dictionary name, and then use brackets to match the key to be changed to the new value. The bright-eyed students will notice that this is like adding data to a dictionary. In fact, we can be very sure to tell you that it is exactly the same, so we can summarize a rhyme: ** there is this key to modify, no add!

The teacher suddenly found that I was wrong in a place when I changed the test paper for Xiaohong. She should have been 100 points, so now we have to modify the dictionary for Xiaohong. The code:

dict = {'line generation': "99"."Data analysis": "99"."Probability theory": "98"}      # create a dictionary

dict['line generation'] = 100

print('Xiao Hong's real grade is:'.dict['line generation'])
print(dict)
Copy the code

(2) Items commonly used!

** update the dictionary, merge the old dictionary with the new dictionary, and overwrite the old dictionary if the key is repeated. ** code:

dict = {'name':'dry'.'age':18.'sex':'male'}
dict.update({'height': 195.'age': 20})
print(dict)
Copy the code

Delete data from dictionary:

(1) Base operation not 6!

In Python programs, for information that is no longer needed in the dictionary, the del statement can be used to remove the corresponding “key value” pair information completely. To delete data from a dictionary using the DEL statement, you must specify the dictionary name and the key to delete.

Now we don’t need to count the score of xiaohong’s line generation, let’s see how I can delete this key value pair. The code:

dict = {'line generation': "99"."Data analysis": "99"."Probability theory": "98"}      # create a dictionary

del dict['line generation']      # delete key 'line'
    
print(dict)           Display the dict elements

Copy the code

(2) Items commonly used!

(1) clear clear dictionary — usage: dictionary name

dict = {'name':'dry'.'age':18.'sex':'male'}
dict.clear()
print(dict)
Copy the code

(2) pop(key) pop(key)

The code:

dict = {'name':'dry'.'age':18.'sex':'male'}
a = dict.pop('name')
print('The pop-up key has the value:', a)
print(dict)
Copy the code

(3) popitem returns and deletes the last pair of keys and values in the dictionary.The code:

dict = {'name':'dry'.'age':18.'sex':'male'}
a = dict.popitem()		# # is equivalent to pushing out, but one key/value pair at a time

print('Delete the last key-value pair in the dictionary:',a)
print(dict)
Copy the code

🔆 In The End!

Start now, stick to it, a little progress a day, in the near future, you will thank you for your efforts!

This blogger will continue to update the basic column of crawler and crawler combat column, carefully read this article friends, you can like the collection and comment on your feelings after reading. And can follow this blogger, read more crawler in the days ahead!

If there are mistakes or inappropriate words can be pointed out in the comment area, thank you! If reprint this article please contact me for my consent, and mark the source and the name of the blogger, thank you!