Is there an easy way to remove a dictionary property in Python when it is possible that the property might not be present to begin with?
if the del operator is used, the result is a KeyError:
a = {} del a['foo'] >>>KeyError: 'foo'
It is not trivial to use an if statement, and you must enter 'foo' and a twice:
a = {} if 'foo' in a: del a['foo']
Look for something similar to dict.get() , which defaults to None if the property is missing:
a = {} a.get('foo') # Returns None, does not raise error
source share