Today, I came across the dict
method get
which, given a key in the dictionary, returns the associated value.
For what purpose is this funct
get
takes a second optional value. If the specified key does not exist in your dictionary, then this value will be returned.
dictionary = {"Name": "Harry", "Age": 17}
dictionary.get('Year', 'No available data')
>> 'No available data'
If you do not give the second parameter, None
will be returned.
If you use indexing as in dictionary['Year']
, nonexistent keys will raise KeyError
.