Skip to content

Instantly share code, notes, and snippets.

@tianchu
Last active October 13, 2021 01:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tianchu/f7835b08d7c788b79ade to your computer and use it in GitHub Desktop.
Save tianchu/f7835b08d7c788b79ade to your computer and use it in GitHub Desktop.
Remove empty fields from a nested dict
def remove_empty_fields(data_):
"""
Recursively remove all empty fields from a nested
dict structure. Note, a non-empty field could turn
into an empty one after its children deleted.
E.g,
{
"location": {
"city": "",
"state": None,
"tags": []
},
"name": "Nick's Caffee",
"reviews": [{}]
}
=>
{
"name": "Nick's Caffee"
}
:param data_: A dict or list.
:return: Data after cleaning.
"""
if isinstance(data_, dict):
for key, value in data_.items():
# Dive into a deeper level.
if isinstance(value, dict) or isinstance(value, list):
value = remove_empty_fields(value)
# Delete the field if it's empty.
if value in ["", None, [], {}]:
del data_[key]
elif isinstance(data_, list):
for index in reversed(range(len(data_))):
value = data_[index]
# Dive into a deeper level.
if isinstance(value, dict) or isinstance(value, list):
value = remove_empty_fields(value)
# Delete the field if it's empty.
if value in ["", None, [], {}]:
data_.pop(index)
return data_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment