Python is a tool for almost everything
Python is well known for his nice syntax and for the easy approach that you can have with it when you encounter it for the first time. After a while you will soon love it and you will find it useful in lot of occasions.
Python dictionary
Python dictionary is a very powerful tool, one of those that make you fall in love with this language. In the latest version 3.6 it has been even improved. Now it’s faster and it is finally ordered. Here you can see some fancy way, in pythonic style, of merging two dictionaries together. Remember that if you insert a key that already exists in the dictionary, this will overwrite the oldest and will substitute it’s value with the newest one.
From python 3.5
This method is avaiable only from version 3.5 of Python. In this version you can do so:
a = {'a': 1, 'b': 2, 'c': 3}
b = {'b': 3, 'c': 4, 'd': 5}
c = {**a, **b}
>>> c
{'a': 1, 'b': 3, 'c': 4, 'd': 5}
Another way
dic1 = {'one':1,'two':2}
dic2 = {'three':3,'four':4}
dic1U2 = {}
for i in dic1.keys():
dic1U2[i] = dic1[i]
for i in dic2.keys():
dic1U2[i] = dic2[i]
print(dic1U2)
output:
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
And … another way eay to read
a = {'a': 1, 'b': 2, 'c': 3}
b = {'b': 3, 'c': 4, 'd': 5}
for eachkey in b:
a[eachkey] = b[eachkey]
print(a)
output:
{'a': 1, 'b': 3, 'c': 4, 'd': 5}