Method Name:
get
Method Signature:
get(dict_key[,default_value])
Method Overview:
- Returns the value associated with a key in a Python dictionary object.
- The get() method differs from dictionary lookup using the [] operator by not raising a KeyError when a given key is not found.
- Also when using get() method to get the corresponding value for a given key, a default value can be specified which is returned when the key is not found.
Parameters:
dict_key – The key for which the value needs to be found in the dictionary.
default_value – This is an optional parameter. If the default value is specified the value is returned when a key is not found in the dictionary object. If this parameter is not specified and the key is not found in the dictionary the get() method returns None.
Exception Handling:
This method will not raise a KeyError when the specified key is not found in the dictionary. Refer to the default_value parameter above.
Example 1:
# Example Python program that retrieves # Create a Python dictionary # Populate the dictionary # Retrieve the value corresponding to the |
Output:
Liverpool Arsenal None |
Example 2:
# Example Python program that accesses print(leagues.get(5)) |
Output:
Florida Cup |