SoFunction
Updated on 2024-11-10

Four ways to remove key-value pairs and elements from Python dictionaries (summary)

When deleting each dictionary some methods are used in the same way as deleting other data that has separate memory, such as del, which clears the memory directly, and clear(), which is the value to clear the variable value. Dictionary deletion we learn from both the dictionary object itself and the key-value pairs in the dictionary.

Delete the dictionary itself

del deletes the dictionary itself from the memory level, making the dictionary object disappear completely. It is also possible to delete the value corresponding to a key in a dictionary, as we will demonstrate below.

dict4 = {'name': 'Tom', 'age': 18}
del dict4['name']  # Delete dictionary, return error if key does not exist
print(dict4)
del dict4  # Delete the entire dictionary
print(dict4)

Returns results:

{'age': 18}

NameError: name 'dict4' is not defined

As you can see from the results, after deleting the key-value pair, the dictionary indicates that a pair of elements is missing, and after deleting the dictionary, the error message becomes that the variable name is undefined, indicating that the program can no longer find the dictionary in memory.

() Delete dictionary key-value pairs

dict4 = {'name': 'Tom', 'age': 18}
('age')  # Delete the corresponding key-value pair in the dictionary, return an error if the key does not exist
print(dict4)
res = ('age222', 'Default return value')  # Set the default return value
print(res, dict4)

{'name': 'Tom'}
Default return value {'name': 'Tom'}

() Delete dictionary key-value pairs

popitem() deletes the dictionary key-value pairs, deletes according to the last set of key-value pairs, and the return value is the meta-ancestor composed of the deleted key-value pairs.

dict4 = {'name': 'Tom', 'age': 18}
res = ()
print(dict4, res)

{'name': 'Tom'} ('age', 18)

() Delete dictionary key-value pairs: empties everything in the dictionary, but does not delete the dictionary itself, del deletes the dictionary itself

dict4 = {'name': 'Tom', 'age': 18}
()
print(dict4)

Return: {}

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 delete key-value pairs and elements of the four methods (summary) of the article is introduced to this, more related Python dictionary to delete the elements of the content of the search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!