Python Dictionary pop() Method

Example

Delete "model" from the dictionary:

car = {
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
car.pop("model")
print(car)

Run Instance

Definition and Usage

The pop() method deletes the specified item from the dictionary.

The value of the deleted item is the return value of this pop() method, please see the following example.

Syntax

dictionary.pop(keyname, defaultvalue)

Parameter Value

Parameter Description
keyname Required. The key name of the item to be deleted.
defaultvalue

Optional. Return value, if the specified key does not exist.

If this parameter is not specified and the item with the specified key is not found, an error will be raised.

More Examples

Example

The value of the deleted item is the return value of the pop() method:

car = {
  "brand": "Porsche",
  "model": "911",
  "year": 1963
}
x = car.pop("model")
print(x)

Run Instance