I have a dictionary as:
default = {\'a\': [\'alpha\'], \'b\': [\'beta\',\'gamma\'], \'g\': []}
I wish to eliminate the empty values as:
To fix your function, change del[k]
to del d[k]
. There is no function to delete values in place from a dictionary.
What you are doing is deleting the variable k
, not changing the dictionary at all. This is why the original dictionary is always returned.
Rewritten, your function might look like:
def remove_empty_keys(d):
for k in d.keys():
if not d[k]:
del d[k]
This assumes you want to eliminate both empty list and None
values, and actually removes any item with a "false" value.