KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
If I have 2 dicts as follows: d1 = {'a': 2, 'b': 4} d2 = {'a': 2, 'b': ''} In order to 'merge' them: dict(d1.items() + d2.items()) results in {'a': 2, 'b': ''} But what should I do if I would like to compare each value of the two dictionaries and only update d2 into d1 if values in d1 are empty/ None / '' ? When the same key exists, I would like to only maintain the numerical value (either from d1 or d2 ) instead of the empty value. If both values are empty, then no problems maintaining the empty value. If both have values, then d1 -value should stay. i.e. d1 = {'a': 2, 'b': 8, 'c': ''} d2 = {'a': 2, 'b': '', 'c': ''} should result in {'a': 2, 'b': 8, 'c': ''} where 8 is not overwritten by '' .
Tags (comma-separated)
Save Edits
Cancel