Delete None values from Python dict

前端 未结 2 1938
轻奢々
轻奢々 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)
    
    0 讨论(0)
  • 2021-02-08 21:13
    result = default.copy()
    result.update((k, v) for k, v in user.iteritems() if v is not None)
    
    0 讨论(0)
提交回复
热议问题