Proper way to remove keys in dictionary with None values in Python

后端 未结 6 1812
暗喜
暗喜 2020-12-05 04:11

What is the proper way to remove keys from a dictionary with value == None in Python?

6条回答
  •  [愿得一人]
    2020-12-05 04:29

    You can also do this using the del command

       f = {};
       f['win']=None
       f
       => {'win': None}
       del f['win']
    

    Or if you want this in a function format you could do:

    def del_none_keys(dict):
        for elem in dict.keys():
            if dict[elem] == None:
               del dict[elem]
    

提交回复
热议问题