Updating a python dictionary while adding to existing keys?

What is wrong with 2 in-place update operations?

myDict2.update(myDict1)
myDict1.update(myDict2)

Explanation: The first update will overwrite the already existing keys with the values from myDict1, and insert all key value pairs in myDict2 which don't exist.

The second update will overwrite the already existing keys in myDict1 with values from myDict2, which are actually the values from myDict1 itself due to the 1st operation. Any new key value pairs inserted will be from the original myDict2.

This of course is conditional to the fact that you don't care about preserving myDict2

Update: With python3, you can do this without having to touch myDict2

myDict1 = {**myDict1, **myDict2, **myDict1}

which would actually be same as

myDict1 = {**myDict2, **myDict1}

Output

{'1': ('3', '2'), '3': ('2', '1'), '2': ('3', '1'), '4': ('5', '2'), '5': ('2', '4')}

The fastest way to merge large dictionaries is to introduce an intermediate object that behaves as though the dicts are merged without actually merging them (see @Raymond Hettinger's answer):

from collections import ChainMap

class MergedMap(ChainMap):
    def __getitem__(self, key):
        result = []
        found = False
        for mapping in self.maps:
            try:
                result.extend(mapping[key])
                found = True
            except KeyError:
                pass
        return result if found else self.__missing__(key)

merged = MergedMap(myDict1, myDict2)

Whether it is applicable depends on how you want to use the combined dict later.

It uses collections.ChainMap from Python 3.3+ for convenience to provide the full MutableMapping interface; you could implement only parts that you use on older Python versions.


I think the most effective way to do it would be something like this:

for k, v in myDict2.iteritems():
    myDict1[k] = myDict1.get(k, ()) + v

But there isn't an update equivalent for what you're looking to do, unfortunately.