Skip to content

Instantly share code, notes, and snippets.

@kolygri
Created March 10, 2020 19:54
Show Gist options
  • Save kolygri/9cc1e0a5b91c2de6242f04abeffbb3d3 to your computer and use it in GitHub Desktop.
Save kolygri/9cc1e0a5b91c2de6242f04abeffbb3d3 to your computer and use it in GitHub Desktop.
def to_dict_simple(d):
"""Returns the model properties as a dict without None values."""
clean = {}
for k, v in list(six.iteritems(d)):
if isinstance(v, list):
clean[k] = list(map(lambda x: to_dict_simple(x) if isinstance(x, dict) else x, v))
elif isinstance(v, dict):
nested = to_dict_simple(v)
if len(list(six.iterkeys(nested))) > 0:
clean[k] = nested
elif v is not None:
clean[k] = v
return clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment