Index
- Definition
- Syntax
- Parameter Values
- Return Values
- [[Python Dictionary get() Method#Python get() method Vs dict [key ] to Access Elements|Python get() Vs dict [key] ]]
Definition
The get()
method returns the value of the item with the specified key.
Syntax
Parameter Values
Parameter | Description |
---|---|
keyname | Required. The keyname of the item you want to return the value from |
value | Optional. A value to return if the specified key does not exist. Default value None |
Return Values
-
If the key is found, get() returns:
- The value related to key if key is in the dictionary.
-
If the key is not found, get() returns:
- None if and value is not specified.
- Value parameter if the key is not found and value is specified.
Example:
Python get() method Vs dict [key] to Access Elements
get()
method returns a default value if the key
is missing.
However, if the key is not found when you use dict[key]
, KeyError
exception is raised.
Output: