In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Python dictionary to delete key pairs and elements of the method what", in daily operation, I believe many people in Python dictionary to delete key pairs and elements of the method what problems there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Python dictionary delete key pairs and elements of the method what" doubts help! Next, please follow the small series to learn together!
There are some methods used to delete each dictionary that are the same as deleting other data that has independent memory, such as del, which directly clears memory, and clear() is the value that clears the variable value. Dictionary deletion We start from two aspects: the dictionary object itself and the key-value pairs in the dictionary.
Delete the dictionary itself.
del is to delete the dictionary itself from the memory level, making the dictionary object disappear completely. You can also delete the corresponding value of a key in the dictionary. Let's demonstrate this.
dict4 = {'name': 'Tom', 'age': 18}del dict4['name'] #Delete dictionary, return error if key does not exist print(dict4)del dict4 #Delete entire dictionary print(dict4)
Return Results:
{'age': 18}
NameError: name 'dict4' is not defined
As can be seen 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 not defined, indicating that the program cannot find the dictionary in memory.
2.pop() deletes dictionary key-value pairs dict4 = {'name':' Tom','age': 18}dict4.pop(' age')#deletes dictionary key-value pairs, returns error if key does not exist print(dict4)res = dict 4.pop ('age22 ', ' default return value')#sets default return value print(res, dict4)
{'name': 'Tom'}
Default return value {'name':'Tom'}
3.popitem() deletes dictionary key-value pairs
popitem() deletes dictionary key-value pairs, deletes the last key-value pair, and returns the ancestor composed of deleted key-value pairs.
dict4 = {'name': 'Tom', 'age': 18}res = dict4.popitem()print(dict4, res)
{'name': 'Tom'} ('age', 18)
4.clear() deletes dictionary key pairs: clears everything in the dictionary, but does not delete the dictionary itself, del deletes the dictionary itself dict4 = {'name': 'Tom', 'age': 18}dict4.clear()print(dict4)
Return: {}
Python dictionary:
1. Dictionaries are another variable container model, each key-value pair is separated by a colon (:), each key-value pair is separated by a comma (,), and the entire dictionary is surrounded by curly braces {};
2. The keys in the dictionary are generally unique. If they are repeated, the latter key-value pair will overwrite the previous one, but the dictionary value does not need to be unique.
3. Values can take any data type, but keys must be immutable types, such as strings, numbers, or tuples, but not lists because lists are mutable.
At this point, the study of "Python dictionary to delete key-value pairs and elements" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.