0
Q:

join two dictionaries python

z = {**x, **y}
3
dict1 = {'a': 10, 'b': 8} 
dict2 = {'d': 6, 'c': 4} 
dict2.update(dict1)
5
dict1 = {'color': 'blue', 'shape': 'square'}
dict2 = {'color': 'red', 'edges': 4}

dict1.update(dict2) #if a key exists in both, it takes the value of the second dict
# dict1 = {'color': 'red', 'shape': 'square', 'edges': 4}
# dict2 is left unchanged
3
z = {**x, **y}
10
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = {**x, **y} #In Python 3.5 or greater only
>>> print(z)
{'a': 1, 'b': 10, 'c': 11}
1
dic0.update(dic1)
0

New to Communities?

Join the community