Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Dictionary merge by updating but not overwriting if value exists

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 ''.

like image 566
siva Avatar asked Jun 15 '11 07:06

siva


People also ask

How do you not overwrite a dictionary in Python?

Add dictionary to dictionary without overwriting in Python. During iteration, for each key-value pair, check if key already exist in dictionary dict_1 or not. If the key already exists in dict_1 and the value for that key is not of list type, create a list and add both values of that key from dict_1 & dict_2 to it.

How do you check if a key exists in a dictionary Python?

Check If Key Exists Using has_key() The has_key() method is a built-in method in Python that returns true if the dict contains the given key, and returns false if it isn't.

How does dictionary update with new value?

In order to update the value of an associated key, Python Dict has in-built method — dict. update() method to update a Python Dictionary. The dict. update() method is used to update a value associated with a key in the input dictionary.

What happens if you assign a new value to a dictionary using a key that already exists?

If the key is already present in the dictionary, it gets overwritten with the new value. The keys can also be passed as keyword arguments to this method with their corresponding values, like dictionary. update(new_key=new_value) .


2 Answers

Just switch the order:

z = dict(d2.items() + d1.items()) 

By the way, you may also be interested in the potentially faster update method.

In Python 3, you have to cast the view objects to lists first:

z = dict(list(d2.items()) + list(d1.items()))  

If you want to special-case empty strings, you can do the following:

def mergeDictsOverwriteEmpty(d1, d2):     res = d2.copy()     for k,v in d2.items():         if k not in d1 or d1[k] == '':             res[k] = v     return res 
like image 176
phihag Avatar answered Sep 21 '22 15:09

phihag


Updates d2 with d1 key/value pairs, but only if d1 value is not None, '' (False):

>>> d1 = dict(a=1, b=None, c=2) >>> d2 = dict(a=None, b=2, c=1) >>> d2.update({k: v for k, v in d1.items() if v}) >>> d2 {'a': 1, 'c': 2, 'b': 2} 

(Use iteritems() instead of items() in Python 2.)

like image 20
Mark Tolonen Avatar answered Sep 17 '22 15:09

Mark Tolonen