Từ điển là các cấu trúc dữ liệu hữu ích trong Python có chứa các cặp khóa: giá trị. are useful data structures in Python that contain key: value pairs.
Có một số cách để loại bỏ một khóa: cặp giá trị từ một từ điển.
1. Sử dụng # declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
5
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
Từ khóa
5 xóa một khóa: Giá trị cặp từ một từ điển. Chỉ cần truy cập giá trị cần được xóa và sử dụng cú pháp sau:# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
del dict[key]
Nếu
7 không tồn tại,# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
8 sẽ bị ném.# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
2. Sử dụng # declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
9
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
Hàm được xây dựng
9 xóa khóa cuối cùng: cặp giá trị trong từ điển. Bạn không thể chỉ định phần tử cần bị xóa. Hàm không có đối số.# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
9 deletes the last key: value pair in a dictionary. You cannot specify the element that needs to be deleted. The function takes no arguments.# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
Một
8 bị ném nếu từ điển trống. Mặt khác, khóa: cặp giá trị được trả về dưới dạng tuple.# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3. Sử dụng dict.popitem[]
2
dict.popitem[]
Hàm được xây dựng
dict.popitem[]
2 xóa một khóa cụ thể: cặp giá trị khỏi từ điển.dict.popitem[]
2 deletes a specific key: value pair from a dictionary.dict.pop[key[, default]]
Hàm có hai đối số:
7: Khóa của giá trị cần được xóa.: the key of the value that needs to be deleted.# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
5: Đây là một đối số tùy chọn. Giá trịdict.popitem[]
5 được trả về nếudict.popitem[]
7 không tồn tại trong từ điển: this is an optional argument. The# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
5 value is returned ifdict.popitem[]
7 does not exist in the dictionary# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
Nếu
7 không tồn tại và đối số# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
5 không được đưa ra, thì 8 sẽ được trả về. Điều này có thể tránh được bằng cách sử dụng đối số thứ hai.# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
Mặt khác, nếu khóa tồn tại, giá trị của phần tử bị loại bỏ được trả về.
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
THẺ LIÊN QUAN
Python
Del
Popitem
nhạc pop
cộng đồng
Phương thức PopItem [] loại bỏ các mục được chèn vào từ điển lần cuối.
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}
Do Python từ điển lưu trữ khóa lưu trữ
Từ điển của Python cho phép bạn lưu trữ các cặp giá trị khóa, sau đó chuyển từ điển một khóa để nhanh chóng lấy giá trị tương ứng của nó. Cụ thể, bạn xây dựng từ điển bằng cách chỉ định ánh xạ một chiều từ các đối tượng khóa đến đối tượng giá trị.
Python3
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.
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.
dict.pop[key[, default]]
6dict.pop[key[, default]]
76# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
dict.pop[key[, default]]
9Từ 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.
# declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # declare a dictionary dict1 = {1: "one", 2: "two", 3: "three", 4: "four"} print["Original Dictionary: ", dict1] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel] # using popitem[] valDel = dict1.popitem[] print["Dictionary after using popitem[]: ", dict1] print["The key:value pair that was removed is: ", valDel]
1
2
3
4
5
6
7
8
5
0dict.pop[key[, default]]
7__
The dictionary before performing remove is : {'Arushi': 22, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Haritha': 21}
6dict.pop[key[, default]]
7dict.pop[key[, default]]
8dict.pop[key[, default]]
9dict.pop[key[, default]]
Traceback [most recent call last]: File "/home/44db951e7011423359af4861d475458a.py", line 20, in del test_dict['Mani'] KeyError: 'Mani'
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
5 # declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
22
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
Đầu ra: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
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
6# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
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}9
5____40# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
8____35__4040373744# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
78Phươ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.
dict.pop[key[, default]]
6dict.pop[key[, default]]
715# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
17# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
Traceback [most recent call last]: File "/home/44db951e7011423359af4861d475458a.py", line 20, in del test_dict['Mani'] KeyError: 'Mani'6
dict.pop[key[, default]]
6dict.pop[key[, default]]
706# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
Chúng ta có thể xóa khóa không
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.
dict.pop[key[, default]]
6dict.pop[key[, default]]
715# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
17# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
Traceback [most recent call last]: File "/home/44db951e7011423359af4861d475458a.py", line 20, in del test_dict['Mani'] KeyError: 'Mani'6
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
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
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
6# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
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}9
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
0__13738# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
8# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
07# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
25# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
0dict.pop[key[, default]]
5dict.pop[key[, default]]
648# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
49# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
50# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
51# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
55# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
57# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
58# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
59# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
60# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
61# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
62# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
63# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
64# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
65# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
6dict.pop[key[, default]]
76# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using pop[]
valDel = dict1.pop[1]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
valDel = dict1.pop[3, "No Key Found"]
print["Dictionary after using pop[]: ", dict1]
print["The value that was removed is: ", valDel]
# using pop[]
# if default value not given, error will be raised
print["Error raised: "]
valDel = dict1.pop[3]
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
74# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
Output:
0# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
Trong ví dụ này, chúng tôi sẽ sử dụng khả năng hiểu từ điển để xóa khóa khỏi từ điển.
Python3
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
6# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
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}9
5____40# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
8____35__4040373744# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
6dict.pop[key[, default]]
796# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
dict.popitem[]
00___dict.pop[key[, default]]
6dict.pop[key[, default]]
7dict.popitem[]
14dict.popitem[]
15Output:
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
Phương pháp 5: Lặp lại và loại bỏ
Trong ví dụ này, chúng tôi sẽ sử dụng một vòng lặp để xóa một khóa khỏi từ điển.
Python3
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
6# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
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}9
5____40# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
8____35__4040373744# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
6dict.popitem[]
36dict.pop[key[, default]]
6dict.pop[key[, default]]
796# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
dict.popitem[]
00___Phương pháp 5: Lặp lại và loại bỏ
Trong ví dụ này, chúng tôi sẽ sử dụng một vòng lặp để xóa một khóa khỏi từ điển.
dict.pop[key[, default]]
6dict.popitem[]
55Output:
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
37# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2 dict.popitem[]
39
dict.popitem[]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.popitem[]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
58 dict.popitem[]
41# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
62 dict.popitem[]
43
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
dict.popitem[]
4464# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
65# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.popitem[]
48dict.popitem[]
49Python3
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
6# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
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}9
5____40# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
8____35__4040373744# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
6dict.popitem[]
36dict.pop[key[, default]]
6dict.pop[key[, default]]
796# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
dict.popitem[]
79dict.popitem[]
49dict.popitem[]
44dict.pop[key[, default]]
6dict.popitem[]
36dict.popitem[]
84dict.popitem[]
49dict.popitem[]
44dict.pop[key[, default]]
6dict.pop[key[, default]]
7dict.popitem[]
89The 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
Output:
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
dict.popitem[]
00___
dict.popitem[]
Phương pháp 5: Lặp lại và loại bỏ
Python3
1# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
2# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
3# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
5# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
6# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
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}9
5____40# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
7# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
8____35__4040373744# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
6dict.popitem[]
3612# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
# using popitem[]
valDel = dict1.popitem[]
print["Dictionary after using popitem[]: ", dict1]
print["The key:value pair that was removed is: ", valDel]
dict.pop[key[, default]]
6dict.pop[key[, default]]
796# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]
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
dict.pop[key[, default]]
6dict.popitem[]
36Output:
4# declare a dictionary
dict1 = {1: "one", 2: "two", 3: "three", 4: "four"}
print["Original Dictionary: ", dict1]
# using del and giving the key as index
del dict1[1]
print["Dictionary after del: ", dict1]
# using del and giving the key as index
del dict1[3]
print["Dictionary after del: ", dict1]