Python辞書pop()メソッド
例
辞書から「モデル」を削除します。
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)