Silently removing key from a python dict
You can try:
d = dict((k, v) for k,v in d.items() if k is not None and k != '')
or to remove all empty-like keys
d = dict((k, v) for k,v in d.items() if k )
You could use the dict.pop
method and ignore the result:
for key in [None, '']:
d.pop(key, None)
The following will delete the keys, if they are present, and it won't throw an error:
for d in [None, '']:
if d in my_dict:
del my_dict[d]
You can do this:
d.pop("", None)
d.pop(None, None)
Pops dictionary with a default value that you ignore.