V S
V S

Reputation: 27

delete empty values in dictionary

My code is as follows:

new_dict = {k: v for k,v in d.items() if v != None}

I need to delete the empty values that appear as '' in my dictionary.

I currently get the output:

'000B4662348C35B4': ['000B4662348C35B4', '', '', '', '', '', '2938717381', '5286676508', '7818174481', '2938717381', '4110479734', '9894624226', '9827417465', '9907632031', '2941365751', '1220277655', '7455720881', '88810260', '8441033464', '3992507902', '8464607083', '1201527184', '2861935553', '4110479734', '5789253700', '2637281600', '5603666228', '9126320955', '2431085055', '8833906919', '1565530436', '8340343124', '2029932640', '6334249086'],

this is just a little bit of the output but you should get the idea. I just want the '' removed.

so far I have tried:

new_dict = {k: v for k,v in d.items() if v != ''}
new_dict = {k: v for k,v in d.items() if v}

all of those still print the same output.

Upvotes: 1

Views: 1464

Answers (3)

Andrej Kesely
Andrej Kesely

Reputation: 195573

You can use list comprehension on dictionary values where you will filter-out empty elements:

d = {'000B4662348C35B4': ['000B4662348C35B4', '', '', '', '', '', '2938717381', '5286676508', '7818174481', '2938717381', '4110479734', '9894624226', '9827417465', '9907632031', '2941365751', '1220277655', '7455720881', '88810260', '8441033464', '3992507902', '8464607083', '1201527184', '2861935553', '4110479734', '5789253700', '2637281600', '5603666228', '9126320955', '2431085055', '8833906919', '1565530436', '8340343124', '2029932640', '6334249086']}

d = {k: [i for i in v if i] for k, v in d.items()}
print(d)

Prints:

{'000B4662348C35B4': ['000B4662348C35B4', '2938717381', '5286676508', '7818174481', '2938717381', '4110479734', '9894624226', '9827417465', '9907632031', '2941365751', '1220277655', '7455720881', '88810260', '8441033464', '3992507902', '8464607083', '1201527184', '2861935553', '4110479734', '5789253700', '2637281600', '5603666228', '9126320955', '2431085055', '8833906919', '1565530436', '8340343124', '2029932640', '6334249086']}

Note:

{k: v for k,v in d.items() if v != ''}

will just filter-out dictionary values that aren't equal to '', but your dictionary values are lists.


EDIT: Without list and dict-comprehensions:

out = {}
for k, v in d.items():
    tmp = []
    for i in v:
        if i != "":
            tmp.append(i)
    out[k] = tmp

print(out)

Upvotes: 5

Umutambyi Gad
Umutambyi Gad

Reputation: 4101

Try the following example

d = {'000B4662348C35B4': ['000B4662348C35B4', '', '', '', '', '', '2938717381', '5286676508', '7818174481', '2938717381', '4110479734', '9894624226', '9827417465', '9907632031', '2941365751', '1220277655', '7455720881', '88810260', '8441033464', '3992507902', '8464607083', '1201527184', '2861935553', '4110479734', '5789253700', '2637281600', '5603666228', '9126320955', '2431085055', '8833906919', '1565530436', '8340343124', '2029932640', '6334249086']}
new_dict = {k: [i for i in v if i != ''] for k,v in d.items()}
print(new_dict)

Explanation

since the v is the list you can't check if the list is empty because it will always return true though you have to iterate on its elements to remove those empty ones like the above example

Upvotes: 0

Samwise
Samwise

Reputation: 71542

If you want this to work with arbitrarily nested lists and dicts, I'd do it via a recursive function:

>>> def remove_falsy_recursive(obj):
...     """Recursively remove all falsy values from a nested dict/list."""
...     if isinstance(obj, dict):
...         return {k: remove_falsy_recursive(v) for k, v in obj.items() if v}
...     if isinstance(obj, list):
...         return [remove_falsy_recursive(i) for i in obj if i]
...     return obj
...
>>> remove_falsy_recursive({'foo': ['bar', '', 'ola', ''], 'bar': False})
{'foo': ['bar', 'ola']}

Note that this uses the Python concept of "falsiness", which includes empty strings, Nones, zeroes, etc. If your requirement for "empty" values doesn't quite match that, adjust as needed.

This is IMO easier to understand than a single nested comprehension, it's a lot more resilient to changes in the structure of the input, and it's fairly easy to extend it to handle other types or conditions.

Upvotes: 0

Related Questions