Search code examples
listpython-3.xdictionaryempty-list

Python3 Removing dictionary key if value contained in list is blank


So I have a dictionary filled with lots of useful stuff. I would like to remove a key (build a new dict without the key) if any value within a list is empty.

The dictionary:

>>>print(vaar123)
{'moo': 'cora', 'ham': ['', 'test'], 'bye': 2, 'pigeon': '', 'heloo': 1}

I can remove the 'pigeon' key with its empty value with something along the lines of.

>>>dict((k, v) for k, v in vaar123.items() if v)
{'moo': 'cora', 'ham': ['', 'test'], 'heloo': 1, 'bye': 2}

But try as I might, I cannot seem to come up with a method to remove 'ham' as it has an empty value in its list.

Thanks in advance for any suggestions, Frank

Info: The dictionary is built with a value on creation (set by admin) the additional value is added to the list by user input. The value pair is used as output. Having a single value in the list produces undesirable output.


Solution

  • This function recursively checks Sized Iterables to see if they are empty and returns False if it finds one that is

    from collections.abc import Sized, Iterable #If you're on Python >= 3.6,
                                                #you can use collections.abc.Collection
    
    def all_nonempty(v):
        if isinstance(v, (Sized, Iterable)):
            return v and (all(map(all_nonempty, v)) if not isinstance(v, str) else True)
            #We do the check against str because 'a' is a sized iterable that
            #contains 'a'.  I don't think there's an abstract class for
            #containers like that
        return True
    

    Then we can use this to winnow the dict

    print({k: v for k, v in d.items() if all_nonempty(v)})
    

    outputs:

    {'moo': 'cora', 'bye': 2, 'heloo': 1}