Is there an easy way to delete a property of a dictionary in Python when it is possible that the property may not be present to begin with?
if the del
statement is used, a KeyError is the result:
a = {}
del a['foo']
>>>KeyError: 'foo'
Its a little wordy to use an if statement and you have to type 'foo'
and a
twice :
a = {}
if 'foo' in a:
del a['foo']
Looking for something similar to dict.get()
that defaults to a None
value if the property is not present:
a = {}
a.get('foo') # Returns None, does not raise error