Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to clean a nested python dict from empty strings and Nones

I created a function that cleans None keys from my dictionary but I couldn’t figure out how to clean recursively if if have dictionaries within my dictionary. I was also able to remove None keys but not empty strings. help?

def clean_dict(dict_with_nones):
    return {key: value for key, value in dict_with_nones.items() if value is not None}

This works well in cleaning Nones with a flat dictionary, I tried to add another condition to remove the empty strings:

def clean_dict(dict_with_nones):
    return {key: value for key, value in dict_with_nones.items() if value is not None or value != ""}

Something about the way I constructed the condition here causes is the None detection not to work as well… I tried surrounding the condition with () but it didnt help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So back to my question, How can I remove both empty strings and Nones from a nested dictionary?

>Solution :

Assuming you really want to clean the keys, not values:

def clean_dict(dict_with_nones):
    if not isinstance(dict_with_nones, dict):
        return dict_with_nones
    return {key: clean_dict(value) for key, value in dict_with_nones.items()
            if key is not None}


test = {None: {'a': 1}, 'b': {'c': {None: 2}, 'd': 3, None: 4}}

out = clean_dict(test)

output:

{'b': {'c': {}, 'd': 3}}

variant to avoid yielding null values:

def clean_dict(dict_with_nones):
    if not isinstance(dict_with_nones, dict):
        return dict_with_nones
    return {key: v
            for key, value in dict_with_nones.items()
            if key is not None and (v:=clean_dict(value))}

out = clean_dict(test)

output:

{'b': {'d': 3}}

removing VALUES

If you meant to remove falsy values (None, empty string, etc.)

def clean_dict_value(d):
    if not isinstance(d, dict):
        return d if d else False # use a custom test if needed
    return {key: v
            for key, value in d.items()
            if (v:=clean_dict_value(value))}


test = {'a': {'b': 1}, 'c': {'d': None, 'e': 3, 'f': {'g': ''}}, 'h': None}

clean_dict_value(test)

output:

{'a': {'b': 1}, 'c': {'e': 3}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading