พจนานุกรม Python ป๊อป ()วิธีการ
ตัวอย่าง
ลบ "model" ออกจากพจนานุกรม:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
ความหมายและการใช้งาน
วิธี การpop()
ลบรายการที่ระบุออกจากพจนานุกรม
ค่าของรายการที่ถูกลบคือผลตอบแทนของpop()
วิธีการ ดูตัวอย่างด้านล่าง
ไวยากรณ์
dictionary.pop(keyname, defaultvalue)
ค่าพารามิเตอร์
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 |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
ค่าของรายการที่ถูกลบคือค่าที่ส่งคืนของเมธอด pop():
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.pop("model")
print(x)