In [52]:
def merge_dicts(*dict_args):
    """ Given any number of dictionaries, merges them into a comprehensive 
    dictionary.  For example:
    
    >>> from collections import OrderedDict
    >>> x = {'a': 1, 'b': 2}
    >>> y = {'b': 3, 'c': 4}
    >>> z = OrderedDict(sorted(merge_dicts(x, y).items(), key=lambda i: i[0]))
    >>> z
    OrderedDict([('a', 1), ('b', 5), ('c', 4)])
    
    """
    result = {}
    
    for dictionary in dict_args:
        for key, value in dictionary.items():
            result[key] = result.get(key, 0) + value
        
    return result

In [50]:
%doctest_mode
>>> from collections import OrderedDict
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}
>>> z = OrderedDict(sorted(merge_dicts(x, y).items(), key=lambda i: i[0]))
>>> z
# OrderedDict([('a', 1), ('b', 5), ('c', 4)])


Exception reporting mode: Plain
Doctest mode is: ON
Out[50]:
OrderedDict([('a', 1), ('b', 5), ('c', 4)])

In [53]:
import doctest
doctest.testmod()


Out[53]:
TestResults(failed=0, attempted=5)