Use a recursive function that returns a new dictionary:
def clean_empty(d):
if isinstance(d, dict):
return {
k: v
for k, v in ((k, clean_empty(v)) for k, v in d.items())
if v
}
if isinstance(d, list):
return [v for v in map(clean_empty, d) if v]
return d
The {..}
construct is a dictionary comprehension; it'll only include keys from the original dictionary if v
is true, e.g. not empty. Similarly the [..]
construct builds a list.
The nested (.. for ..)
construct is a generator expression that allows the code to compactly filter empty objects after recursing.
Another way of constructing such a function is to use the @singledispatch
decorator; you then write multiple functions, one per object type:
from functools import singledispatch
@singledispatch
def clean_empty(obj):
return obj
@clean_empty.register
def _dicts(d: dict):
items = ((k, clean_empty(v)) for k, v in d.items())
return {k: v for k, v in items if v}
@clean_empty.register
def _lists(l: list):
items = map(clean_empty, l)
return [v for v in items if v]
The above @singledispatch
version does exactly the same thing as the first function but the isinstance()
tests are now taken care of by the decorator implementation, based on the type annotations of the registered functions. I also put the nested iterators (the generator expression and map()
function) into a separate variable to improve readability further.
Note that any values set to numeric 0 (integer 0, float 0.0) will also be cleared. You can retain numeric 0 values with if v or v == 0
.
Demo of the first function:
>>> sample = {
... "fruit": [
... {"apple": 1},
... {"banana": None}
... ],
... "veg": [],
... "result": {
... "apple": 1,
... "banana": None
... }
... }
>>> def clean_empty(d):
... if isinstance(d, dict):
... return {
... k: v
... for k, v in ((k, clean_empty(v)) for k, v in d.items())
... if v
... }
... if isinstance(d, list):
... return [v for v in map(clean_empty, d) if v]
... return d
...
>>> clean_empty(sample)
{'fruit': [{'apple': 1}], 'result': {'apple': 1}}