Python Dictionary pop() Metode
Contoh
Hapus "model" dari kamus:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
Definisi dan Penggunaan
Metode pop()
ini menghapus item yang ditentukan dari kamus.
Nilai item yang dihapus adalah nilai pengembalian pop()
metode, lihat contoh di bawah.
Sintaksis
dictionary.pop(keyname, defaultvalue)
Nilai Parameter
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 |
Lebih Banyak Contoh
Contoh
Nilai item yang dihapus adalah nilai kembalian dari metode pop():
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.pop("model")
print(x)