Hướng dẫn how to delete a key from ordered dictionary in python - cách xóa khóa khỏi từ điển đã đặt hàng trong python

Tôi đang cố gắng xóa một khóa và giá trị khỏi

del dct[key]
3 nhưng khi tôi sử dụng:

Show
dictionary.popitem(key)

Nó loại bỏ khóa và giá trị cuối cùng ngay cả khi một khóa khác được cung cấp. Có thể xóa một khóa ở giữa nếu từ điển?

Đã hỏi ngày 26 tháng 11 năm 2014 lúc 17:53Nov 26, 2014 at 17:53

Hướng dẫn how to delete a key from ordered dictionary in python - cách xóa khóa khỏi từ điển đã đặt hàng trong python

5

Có, bạn có thể sử dụng

del dct[key]
4:

del dct[key]

Dưới đây là một cuộc biểu tình:

>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>

Trên thực tế, bạn phải luôn luôn sử dụng

del dct[key]
4 để loại bỏ một mục khỏi từ điển.
del dct[key]
6 và
del dct[key]
7 được sử dụng để loại bỏ một mục và trả lại mục đã xóa để có thể lưu sau này. Tuy nhiên, nếu bạn không cần lưu nó, thì sử dụng các phương pháp này là kém hiệu quả.and return the removed item so that it can be saved for later. If you do not need to save it however, then using these methods is less efficient.

Đã trả lời ngày 26 tháng 11 năm 2014 lúc 17:54Nov 26, 2014 at 17:54

Bạn có thể sử dụng pop, popitem loại bỏ lần cuối cùng theo mặc định:

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)

Đã trả lời ngày 26 tháng 11 năm 2014 lúc 17:55Nov 26, 2014 at 17:55

Hướng dẫn how to delete a key from ordered dictionary in python - cách xóa khóa khỏi từ điển đã đặt hàng trong python

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
del dct[key]
83
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

Example:

Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}

del dct[key] 87del dct[key] 9 del dct[key] 89del dct[key] 45 del dct[key] 91del dct[key] 49 del dct[key] 8del dct[key] 51

Phương pháp 5: Lặp lại và loại bỏ

Python3

del dct[key]
8
del dct[key]
9
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
0
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
1
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
2
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
4
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
5__22227272424____29__2222727

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
5
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
6

del dct[key]
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
8
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
9
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
0

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
6

del dct[key]
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
8
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
9
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
0

Đầu ra:

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}

Ngoại lệ: & nbsp;

Traceback (most recent call last):
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'

Phương pháp 2: Xóa một khóa khỏi từ điển bằng pop () & nbsp;

Pop () có thể được sử dụng để xóa khóa và giá trị của nó tại chỗ. Ưu điểm sử dụng DEL là nó cung cấp cơ chế để in giá trị mong muốn nếu cố gắng loại bỏ một dict không tồn tại. đôi. Thứ hai, nó cũng trả về giá trị của khóa đang được gỡ bỏ ngoài việc thực hiện một thao tác xóa đơn giản. Thể hiện xóa cặp giá trị khóa bằng pop () & nbsp;pop() can be used to delete a key and its value inplace. The advantage over using del is that it provides the mechanism to print desired value if tried to remove a non-existing dict. pair. Second, it also returns the value of the key that is being removed in addition to performing a simple delete operation. Demonstrating key-value pair deletion using pop() 

Python3

Các

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
5
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
4
del dct[key]
9
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
6
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
9
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
8

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
3
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
7
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : 
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after performing remove is : 
 {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
0

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
The dictionary before performing remove is : 
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after performing remove is : 
 {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
3
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
8

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
3
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
7
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : 
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after performing remove is : 
 {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
0

Output:

The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found

Phương pháp 3: Sử dụng các mục () + Dictribution để xóa khóa khỏi từ điển

Python3

Các mục () cùng với sự hiểu biết của Dict cũng có thể giúp chúng ta đạt được nhiệm vụ xóa cặp giá trị khóa, nhưng, nó có nhược điểm của việc không phải là một điều kiện tại chỗ. kĩ thuật. Trên thực tế, một dict mới được tạo ra ngoại trừ chìa khóa mà chúng tôi không muốn bao gồm. Thể hiện Xóa cặp giá trị khóa bằng cách sử dụng các mục () + Dictlement.

del dct[key]
25
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
5
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
2
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
7
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
4
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
9
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
2
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
7
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
2

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
del dct[key]
35

del dct[key]
8
del dct[key]
9
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
0
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
1
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
2
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
6
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
2227____
del dct[key]
24

del dct[key]
36
del dct[key]
37
del dct[key]
38
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

del dct[key]
42
del dct[key]
9
del dct[key]
44
del dct[key]
45
del dct[key]
46

del dct[key]
47
del dct[key]
48
del dct[key]
49
del dct[key]
50
del dct[key]
51
del dct[key]
52__

Output:

The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}

d = OrderedDict([(1,2),(3,4)]) d.pop(your_key) 3d = OrderedDict([(1,2),(3,4)]) d.pop(your_key) 4Before remove key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21} Operation Perform: del test_dict['Mani'] After removing key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}3 The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found1 The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found2del dct[key] 61

Phương pháp 4: Sử dụng khả năng hiểu từ điển Python để xóa khóa khỏi từ điển

Python3

Các

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
5
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
4
del dct[key]
9
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
6
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
9
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
8

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
01
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
02

Output:

The dictionary before performing remove is : 
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after performing remove is : 
 {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}

d = OrderedDict([(1,2),(3,4)]) d.pop(your_key) 3d = OrderedDict([(1,2),(3,4)]) d.pop(your_key) 4Before remove key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21} Operation Perform: del test_dict['Mani'] After removing key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}3 The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found1 The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found2The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found3

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
7
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : 
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after performing remove is : 
 {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
0

Python3

Các

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
23

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
5
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
4
del dct[key]
9
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
6
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
9
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
8

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
3
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
7
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : 
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after performing remove is : 
 {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
0

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
42

Output:

del dct[key]
0

Phương pháp 3: Sử dụng các mục () + Dictribution để xóa khóa khỏi từ điển

Các mục () cùng với sự hiểu biết của Dict cũng có thể giúp chúng ta đạt được nhiệm vụ xóa cặp giá trị khóa, nhưng, nó có nhược điểm của việc không phải là một điều kiện tại chỗ. kĩ thuật. Trên thực tế, một dict mới được tạo ra ngoại trừ chìa khóa mà chúng tôi không muốn bao gồm. Thể hiện Xóa cặp giá trị khóa bằng cách sử dụng các mục () + Dictlement.

Python3

Các

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
23

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
5
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
66
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
36

>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
31
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
23

>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
71
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
36

>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
31
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
76
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
8

Output:

del dct[key]
1

The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found4del dct[key] 9 The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found6d = OrderedDict([(1,2),(3,4)]) d.pop(your_key) 9The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found8

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
4
Before remove key:   {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}

Operation Perform:   del test_dict['Mani']

After removing key:  {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}
3
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
1
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
2
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21,
 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found
3

Python3

Các

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
23

>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
99

Các

d = OrderedDict([(1,2),(3,4)])
d.pop(your_key)
3
>>> from collections import OrderedDict
>>> dct = OrderedDict()
>>> dct['a'] = 1
>>> dct['b'] = 2
>>> dct['c'] = 3
>>> dct
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> del dct['b']
>>> dct
OrderedDict([('a', 1), ('c', 3)])
>>>
23

Output:

del dct[key]
2