Phương thức pop () từ điển Python
Thí dụ
Xóa "mô hình" khỏi từ điển:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
Định nghĩa và Cách sử dụng
Phương pop()
thức xóa mục đã chỉ định khỏi từ điển.
Giá trị của mục bị loại bỏ là giá trị trả về của pop()
phương thức, xem ví dụ bên dưới.
Cú pháp
dictionary.pop(keyname, defaultvalue)
Giá trị tham số
Parameter | Description |
---|---|
keyname | Required. The keyname of the item you want to remove |
defaultvalue | Optional. A value to return if the specified key do not exist. If this parameter is not specified, and the no item with the specified key is found, an error is raised |
Các ví dụ khác
Thí dụ
Giá trị của mục đã loại bỏ là giá trị trả về của phương thức pop ():
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.pop("model")
print(x)