Newbie to Python, so this may seem silly.
I have two dicts:
default = {'a': 'alpha', 'b': 'beta', 'g': 'Gamma'}
user = {'a': 'NewAlpha', 'b': None}
I need to update my defaults with the values that exist in user. But only for those that have a value not equal to None. So I need to get back a new dict:
result = {'a': 'NewAlpha', 'b': 'beta', 'g': 'Gamma'}
result = default.copy()
result.update((k, v) for k, v in user.iteritems() if v is not None)
With the update()
method, and some generator expression:
D.update((k, v) for k, v in user.iteritems() if v is not None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With