def _clean_empty(d):
    """Remove None values from a dict."""
    if not isinstance(d, (dict, list)):
        return d
    if isinstance(d, list):
        return [v for v in (_clean_empty(v) for v in d) if v is not None]
    return {
        k: v for k, v in
        ((k, _clean_empty(v)) for k, v in d.items())
        if v is not None
    }
Remove None values from a dict.