I have a dictionary like:
default = {'a': ['alpha'], 'b': ['beta','gamma'], 'g': []}
I want to remove empty values ββas:
default = {'a': ['alpha'], 'b': ['beta','gamma']}
I wrote a function (following an example found on the Internet)
def remove_empty_keys(d):
for k in d.keys():
try:
if len(d[k]) < 1:
del[k]
except:
pass
return(d)
I have the following questions:
1- I did not find an error, why it always returns the following -
remove_empty_keys(default)
{'a': ['alpha'], 'b': ['beta'], 'g': []}
2 Is there a built-in function to remove / remove Null / None / empty values ββfrom a Python dictionary without creating a copy of the original dictionary?
source
share