Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: An elegant way to delete empty lists from Python dictionary

I have a dictionary as:

default = {'a': ['alpha'], 'b': ['beta','gamma'], 'g': []}

I wish to eliminate the empty values as:

default = {'a': ['alpha'], 'b': ['beta','gamma']}

I wrote a function (following an example found on the web)

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 didn't find the mistake why it always returns following -

remove_empty_keys(default)
 {'a': ['alpha'], 'b': ['beta'], 'g': []}

2- Is there a built-in function to eliminate/delete Null/None/empty values from Python dictionary without creating a copy of the original dictionary?

like image 675
Gianni Spear Avatar asked Feb 11 '13 13:02

Gianni Spear


2 Answers

There's no builtin for this (AFAIK), but you can do it easily with a dict comprehension:

new_dict = {k:v for k,v in original_dict.items() if v}

If you're stuck with an older version of python (pre 2.7 without dict comprehensions), you can use the dict constructor:

new_dict = dict((k,v) for k,v in original_dict.items() if v)

Note that this doesn't operate in place (as per your second question). And dictionaries don't support slice assignment like lists do, so the best* you can really do to get this all done in place is:

new_dict = {k:v for k,v in original_dict.items() if v}
original_dict.clear()
original_dict.update(new_dict)

*of course the term "best" is completely subjective.

like image 165
mgilson Avatar answered Oct 13 '22 04:10

mgilson


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.

like image 36
Michael J. Barber Avatar answered Oct 13 '22 05:10

Michael J. Barber