Hướng dẫn python dict remove key and value - python dict xóa khóa và giá trị

Từ điển được sử dụng trong các ứng dụng thực tế đa dạng như lập trình hàng ngày, phát triển web và lập trình AI/ML, làm cho nó trở thành một container hữu ích nói chung. Do đó, biết các tốc ký để đạt được các nhiệm vụ khác nhau liên quan đến việc sử dụng từ điển luôn luôn là một điểm cộng. Bài viết này đề cập đến một nhiệm vụ như vậy là xóa/xóa một cặp giá trị khóa từ điển khỏi từ điển, chúng tôi sẽ thảo luận về các phương pháp khác nhau để xử lý nhiệm vụ được đưa ra và cuối cùng chúng tôi sẽ xem làm thế nào chúng tôi có thể xóa tất cả các khóa khỏi từ điển.

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}

Phương pháp 1: Xóa một phím khỏi từ điển bằng cách sử dụng DEL

Từ khóa DEL có thể được sử dụng để xóa tại chỗ xóa khóa có trong từ điển trong Python. Một nhược điểm có thể được nghĩ đến việc sử dụng điều này là nó làm tăng một ngoại lệ nếu không tìm thấy chìa khóa và do đó không tồn tại của khóa phải được xử lý. Thể hiện việc xóa cặp giá trị khóa bằng DEL.

Python3

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
6
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5__

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

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
8
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
0
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

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
7

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
8
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
0
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

Đầ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

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
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, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5__16

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
6
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
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
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
4
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Deleted!
8
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
1

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
4
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
7
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
9
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5test_dict 1
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
4
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Deleted!
8
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
1

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

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
7
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
9
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5test_dict 1
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

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.

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
16
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
6
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
3

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
26

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
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, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
15

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
27
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
28
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
29
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
33
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
35
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
36
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
37

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
38
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
39
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
40
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
41
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
42
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
43__

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}

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
4
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
52

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

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
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, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5__16

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
6
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
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
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
92
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
93

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}

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
4
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Deleted!
8
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
1

Python3

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
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, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5__16

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

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
6
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
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
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
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
4
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
4

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Deleted!
8
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
2
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
3
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
1

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

Output:

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
5
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
7
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}
9
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5test_dict 1
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

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

Từ khóa DEL cũng có thể được sử dụng để xóa danh sách, cắt danh sách, xóa từ điển, xóa các cặp giá trị khóa khỏi từ điển, xóa các biến, v.v.

Python3

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
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, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5__16

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

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

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

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

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

Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
22
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
4
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
5
Traceback [most recent call last]:
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Mani']
KeyError: 'Mani'
67
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
{'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
9

Output:

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Deleted!

Phương pháp 2: Xóa tất cả các khóa khỏi từ điển bằng dict.clear []

Phương thức rõ ràng [] loại bỏ tất cả các mục khỏi từ điển. Phương thức rõ ràng [] không trả về bất kỳ giá trị nào.

Python3

test_dict

The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
0
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
1
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
2
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
4
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5
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, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
3
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
8
The dictionary before performing remove is :  {'Arushi': 22, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is :  {'Arushi': 22, 'Haritha': 21}
5__16

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

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

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

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

Output:

{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
Length 0
{}

Bạn có thể xóa một chìa khóa khỏi một python từ điển không?

Để xóa một khóa khỏi từ điển trong Python, hãy sử dụng phương thức pop [] hoặc từ khóa của Del Del. Cả hai phương pháp đều hoạt động giống nhau ở chỗ chúng loại bỏ các khóa khỏi từ điển. Phương thức pop [] chấp nhận một tên khóa là đối số trong khi đó, Del Del chấp nhận một mục từ điển sau từ khóa DEL.use the pop[] method or the “del” keyword. Both methods work the same in that they remove keys from a dictionary. The pop[] method accepts a key name as argument whereas “del” accepts a dictionary item after the del keyword.

Làm cách nào để loại bỏ một giá trị khỏi khóa trong Python?

Trong lớp từ điển Python cung cấp một phương thức pop [] để xóa một mục khỏi từ điển dựa trên khóa, tức là nếu khóa đã cho tồn tại trong từ điển thì dict.pop [] sẽ loại bỏ phần tử bằng khóa đã cho và trả về giá trị của nó. Nếu khóa đã cho không tồn tại trong từ điển thì nó sẽ trả về giá trị mặc định đã cho.dict. pop[] removes the element with the given key and return its value. If the given key doesn't exist in the dictionary then it returns the given Default value.

Làm thế nào để bạn xóa một giá trị cụ thể từ một từ điển trong Python?

Bạn có thể sử dụng các phương thức sau để loại bỏ các mục khỏi từ điển trong Python:..
Từ khóa Del ..
Phương pháp rõ ràng [] ..
Phương pháp pop [] ..
Phương thức Popitem [] ..

Làm cách nào để xóa một khóa khỏi danh sách trong Python?

Trong Python, sử dụng các phương thức danh sách rõ ràng [], pop [] và xóa [] để xóa các mục [phần tử] khỏi danh sách.Cũng có thể xóa các mục bằng cách sử dụng câu lệnh DEL bằng cách chỉ định một vị trí hoặc phạm vi có chỉ mục hoặc lát cắt.use list methods clear[] , pop[] , and remove[] to remove items [elements] from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

Bài Viết Liên Quan

Chủ Đề