I have replied on SO, but I thought I would also reply here with more details to help you out
As you have discovered, dictionaries in python will raise a KeyError
when you try to access the value for a key that doesnât exist in the dictionary. But dictionaries have a built in method called get
, which lets you specify a default for when the key doesnât exist. This avoids the need to first check if key in dict:
etc.