A dictionary maps keys to values.
Each key-value pair represents a single dictionary item.
Traditionally, to access a value of a given dictionary item, we use the key name enclosed in square brackets, like in the example shown below:
cities = {'Ottawa': 'Canada', 'Tokyo': 'Japan', 'Nairobi': 'Kenya', 'Manilla': 'Philippines'}
#access the value associated with a key
result = cities['Nairobi']
print(result)
In the above example, we have a dictionary in which each items key is a city and its value is the country.
We have accessed the value of the item whose key is 'Nairobi' by enclosing the key with square brackets.
In cases where we use the bracket notation with a key that does not exist in the dictionary, a KeyError
exception is raised.
cities = {'Ottawa': 'Canada', 'Tokyo': 'Japan', 'Nairobi': 'Kenya', 'Manilla': 'Philippines'}
#using a non-existent key
cities['Moscow']
As you can see in the above example, a KeyError
is raised because an item with 'Moscow' as key does not exist in the dictionary.
The get()
method of dictionaries helps to avoid the KeyError
without manually using error-handling mechanisms such as try-except blocks.
Using the get()
method
The get()
method also retrieves a value that is associated with a key given as an argument.
cities = {'Ottawa': 'Canada', 'Tokyo': 'Japan', 'Nairobi': 'Kenya', 'Manilla': 'Philippines'}
#access the value associated with a key
result = cities.get('Tokyo')
print(result)
However, unlike with the bracket notation, the get()
method does not raise the KeyError
exception if an item with the given key does not exist in the dictionary. Instead, it returns None
.
cities = {'Ottawa': 'Canada', 'Tokyo': 'Japan', 'Nairobi': 'Kenya', 'Manilla': 'Philippines'}
#using a non-existent key
result = cities.get('Moscow')
print(result)
The get()
method is especially helpful when we are not certain whether a key exists in the dictionary or not as it avoids the need for implementing manual error-handling in case the key is not found.
Setting a default value
The get()
method allows a second argument that specifies a default value that will be returned if the key does not exist. This is not possible when using the bracket notation.
D.get(key, default = None)
If a default value is given, it will be returned instead of None
if the given key
is not found in the dictionary.
get() with default
cities = {'Ottawa': 'Canada', 'Tokyo': 'Japan', 'Nairobi': 'Kenya', 'Manilla': 'Philippines'}
#using a non-existent key
result = cities.get('Moscow', 'Given Key Does Not Exist')
print(result)
In the above example, we specified a default text, you can specify any type of object as the default value not just texts.
Conclusion
- Both the bracket notation and the get method can be used to access a value using the key.
- The bracket notation raises a
KeyError
exception if the target key does not exist in the dictionary. - The get() method returns
None
if the target key does not exist. - You can specify a default value to the
get()
method, so that the value will be returned instead ofNone
.