Python Dictionary get() Method
Example
Get the value of the "model" item:
car = { "brand": "Porsche", "model": "911", "year": 1963 } x = car.get("model") print(x)
Definition and Usage
The get() method returns the value of the item with the specified key.
Syntax
dictionary.get(keyname, value)
Parameter Value
Parameter | Description |
---|---|
keyname | Required. The key name of the item from which you want to return a value. |
value | Optional. Returns a value if the specified key does not exist. Default value None. |
More Examples
Example
Attempt to return the value of a non-existent item:
car = { "brand": "Porsche", "model": "911", "year": 1963 } x = car.get("price", 15000) print(x)