You could use a dict
object's get()
method, as others have already suggested. Alternatively, depending on exactly what you're doing, you might be able use a try/except
suite like this:
try:
<to do something with d[key]>
except KeyError:
<deal with it not being there>
Which is considered to be a very "Pythonic" approach to handling the case.