Home  >  Article  >  Backend Development  >  How to delete elements from python dictionary? How to clear the dictionary?

How to delete elements from python dictionary? How to clear the dictionary?

乌拉乌拉~
乌拉乌拉~Original
2018-08-18 14:07:104123browse

In today's article, we will learn about deleting dictionary elements in python. In this article, I will explain how to delete dictionary elements in python, and how to delete all elements in the dictionary. Okay, without further ado, let’s get started with the article.

Delete dictionary elements

You can delete a single element or clear the dictionary. Clearing only requires one operation.

Display the del command to delete a dictionary, as shown in the following example:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

del dict['Name'];  # 删除键是'Name'的条目
dict.clear();  # 清空词典所有条目
del dict;  # 删除词典

print "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];

But this will cause an exception because the dictionary no longer exists after using del:

dict['Age']:
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print "dict[&#39;Age&#39;]: ", dict[&#39;Age&#39;];
TypeError: &#39;type&#39; object is unsubscriptable

Characteristics of dictionary keys

Dictionary values ​​can take any python object without restrictions, either standard objects or user-defined, but keys cannot.

Two important points need to be remembered:

(1). The same key is not allowed to appear twice. If the same key is assigned twice during creation, the latter value will be remembered, as shown in the following example:

# !/usr/bin/python

dict = {&#39;Name&#39;: &#39;Zara&#39;, &#39;Age&#39;: 7, &#39;Name&#39;: &#39;Manni&#39;};

print "dict[&#39;Name&#39;]: ", dict[&#39;Name&#39;];

The output result of the above example is:

dict[&#39;Name&#39;]:  Manni

(2). The key must be immutable, Therefore, it can be used as a number, string or tuple, so a list will not work. The following example:

# !/usr/bin/python
dict = {[&#39;Name&#39;]: &#39;Zara&#39;, &#39;Age&#39;: 7};
print "dict[&#39;Name&#39;]: ", dict[&#39;Name&#39;];

The output result of the above example:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    dict = {[&#39;Name&#39;]: &#39;Zara&#39;, &#39;Age&#39;: 7};
TypeError: list objects are unhashable

The above is all the content of this article , dictionary element deletion in python. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of How to delete elements from python dictionary? How to clear the dictionary?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more