I have two Python dictionaries, and I want to merge (union) them together but with a custom update function in case the same key exists in both dicts. For instance:
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = {'a': 3, 'd': 5}
>>> merge(x,y,z) # add values together if key exists in both
{'a': 4, 'b': 12, 'c': 11, 'd': 5}
What's the simplest way to do so?
Edit: I'm looking for a solution that works for arbitrary number of dicts to merge, and perform arbitrary operation when the same key is in more than 1 dict (not necessarily add).