去除字典中值为None的元素

 
def remove_None_value_elements(input_dict):
    """
    remove the element(key/value) from dict if the value is None
    :param input_dict:
    :return: new dict
    """
    if type(input_dict) is not dict:
        return None
    result = {}
    for key in input_dict:
        tmp = {}
        if input_dict[key] is not None:
            if type(input_dict[key]).__name__ == 'dict':
                tmp.update({key: remove_None_value_elements(input_dict[key])})
            else:
                tmp.update({key: input_dict[key]})
        result.update(tmp)
    return result

支持输入的字典包含子字典。

原文地址:https://www.cnblogs.com/tlmn2008/p/9760736.html