I\'m currently re-engaging with Python after a long absence and loving it. However, I find myself coming across a pattern over and over. I keep thinking that there must be a
You can also take advantage of the control structure in exception handling. A KeyError exception is thrown by a dictionary when you try to assign a value to a non-existent key:
my_dict = {}
try:
my_dict['a'] += 1
except KeyError, err: # in 2.6: `except KeyError as err:`
my_dict['a'] = 1