# Python 3.9
z = x | y
# Python 3.5
z = {**x, **y}
# Python <= 3.4
def merge_two_dicts(x, y):
z = x.copy() # start with keys and values of x
z.update(y) # modifies z with keys and values of y
return z
z = merge_two_dicts(x, y)
yusuke_power = {"Yusuke Urameshi": "Spirit Gun"}
hiei_power = {"Hiei": "Jagan Eye"}
powers = dict()
# Brute force
for dictionary in (yusuke_power, hiei_power):
for key, value in dictionary.items():
powers[key] = value
# Dictionary Comprehension
powers = {key: value for d in (yusuke_power, hiei_power) for key, value in d.items()}
# Copy and update
powers = yusuke_power.copy()
powers.update(hiei_power)
# Dictionary unpacking (Python 3.5+)
powers = {**yusuke_power, **hiei_power}
# Backwards compatible function for any number of dicts
def merge_dicts(*dicts: dict):
merged_dict = dict()
for dictionary in dicts:
merge_dict.update(dictionary)
return merged_dict