Hướng dẫn can you remove key value pair from dictionary python? - bạn có thể xóa cặp giá trị khóa khỏi python từ điển không?

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

Từ khóa

# 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 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:

del dict[key]

Nếu

# 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]

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]

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

Hàm được xây 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 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.

dict.popitem[]

Một

# 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 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 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

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ố:

  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]

    7: Khóa của giá trị cần được xóa.: the key of the value that needs to be deleted.

  2. dict.popitem[]
    
    5: Đây là một đối số tùy chọn. Giá trị
    dict.popitem[]
    
    5 được trả về nếu

    # 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]

    7 không tồn tại trong từ điển
    : this is an optional argument. The
    dict.popitem[]
    
    5 value is returned if

    # 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]

    7 does not exist in the dictionary

Nếu

# 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]

7 không tồn tại và đối số
dict.popitem[]
5 không được đưa ra, thì

# 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ẽ được trả về. Điều này có thể tránh được bằng cách sử dụng đối số thứ hai.

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]]
6
dict.pop[key[, default]]
7

# 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]

6
dict.pop[key[, default]]
9

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.

# 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

# 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]

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
dict.pop[key[, default]]
0

# 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__

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

dict.pop[key[, default]]
6
dict.pop[key[, default]]
7
dict.pop[key[, default]]
8
dict.pop[key[, default]]
9

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

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

# 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

# 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
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

# 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____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 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
dict.popitem[]
78

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.

dict.pop[key[, default]]
6
dict.pop[key[, default]]
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]

15

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

dict.pop[key[, default]]
6
dict.pop[key[, default]]
7

# 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]

06
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

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]]
6
dict.pop[key[, default]]
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]

15

# 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
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

# 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

# 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
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

# 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
dict.pop[key[, default]]
0__137

# 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]

38

# 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

# 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
dict.pop[key[, default]]
0

# 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
dict.pop[key[, default]]
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]

5
dict.pop[key[, default]]
0
dict.pop[key[, default]]
5

dict.pop[key[, default]]
6

# 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]

48

# 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
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

# 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]

55

# 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 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]

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 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.pop[key[, default]]
6
dict.pop[key[, default]]
7

# 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]

6
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

# 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]

74

Output:

# 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]

0

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

# 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

# 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
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

# 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____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

dict.pop[key[, default]]
6
dict.pop[key[, default]]
7

# 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]

96
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]]
6
dict.pop[key[, default]]
7
dict.popitem[]
14
dict.popitem[]
15

Output:

# 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]

1

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

# 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

# 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
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

# 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____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

dict.pop[key[, default]]
6
dict.popitem[]
36

dict.pop[key[, default]]
6
dict.pop[key[, default]]
7

# 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]

96
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]]
6
dict.popitem[]
55

Output:

# 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

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

# 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

dict.popitem[]
44

# 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 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[]
48
dict.popitem[]
49

Python3

# 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

# 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
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

# 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____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

dict.pop[key[, default]]
6
dict.popitem[]
36

dict.pop[key[, default]]
6
dict.pop[key[, default]]
7

# 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]

96
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[]
79
dict.popitem[]
49

dict.popitem[]
44
dict.pop[key[, default]]
6
dict.popitem[]
36

dict.popitem[]
84
dict.popitem[]
49

dict.popitem[]
44
dict.pop[key[, default]]
6
dict.pop[key[, default]]
7
dict.popitem[]
89
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

Output:

# 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]

3

dict.popitem[]
00___

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

Python3

# 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

# 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
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

# 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____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

dict.pop[key[, default]]
6
dict.popitem[]
36

# 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]

12

dict.pop[key[, default]]
6
dict.pop[key[, default]]
7

# 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]

96
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]]
6
dict.popitem[]
36

Output:

# 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]

4

Chúng ta có thể xóa khóa không

Phương pháp 1: Xóa một khóa khỏi từ điển bằng cách sử dụng 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.Remove a Key from a Dictionary using the del The del keyword can be used to in-place delete the key that is present in the dictionary in Python.

Làm thế nào để bạn xóa một khóa

Để xóa một khóa, cặp giá trị trong từ điển, bạn có thể sử dụng phương thức DEL.Một bất lợi là nó mang lại cho KeyError nếu bạn cố gắng xóa khóa không tồn tại.Vì vậy, thay vì câu lệnh DEL, bạn có thể sử dụng phương thức POP.use the del method. A disadvantage is that it gives KeyError if you try to delete a nonexistent key. So, instead of the del statement you can use the pop method.

Phương pháp từ điển nào được sử dụng để loại bỏ phím được chèn cuối cù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.popitem[] method removes the item that was last inserted into the dictionary.

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ị., and then pass the dictionary a key to quickly retrieve its corresponding value. Specifically, you construct the dictionary by specifying one-way mappings from key-objects to value-objects.

Bài Viết Liên Quan

Chủ Đề