Delete None values from Python dict

前端 未结 2 1936
轻奢々
轻奢々 2021-02-08 20:47

Newbie to Python, so this may seem silly.

I have two dicts:

default = {\'a\': \'alpha\', \'b\': \'beta\', \'g\': \'Gamma\'}
user = {\'a\': \'NewAlpha\',          


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 20:58

    With the update() method, and some generator expression:

    D.update((k, v) for k, v in user.iteritems() if v is not None)
    

提交回复
热议问题