当有字典x和字典y,想合并为字典z。


Python 3.9 + 提供了最简单的方法:
z = x | y # NOTE: 3.9+ ONLY


Python 3.5 + 的方法:
z = {**x, **y}


Python 3.4 or lower 的方法,使用update方法写一个合并方法:

def merge_dicts(*dict_args):
    """
    Given any number of dictionaries, shallow copy and merge into a new dict,
    precedence goes to key-value pairs in latter dictionaries.
    """
    result = {}
    for dictionary in dict_args:
        result.update(dictionary)
    return result

或者直接:
z = x.update( y )