handle key doesn't exists in dictionary in python code example
Example 1: python how to check if a dictionary key exists
if word in data:
return data[word]
else:
return "The word doesn't exist. Please double check it."
Example 2: add key if not exists python
# credit to Stack Overflow user in the source link
# d is your dictionary
if key not in d:
d[key] = value