combine two maps in python

To combine two maps(a.k.a dictionaries) in python, we could use the update() method of the dictionary.

Here's an example:

main.py
map1 = {'a': 1, 'b': 2}
map2 = {'c': 3, 'd': 4}

map1.update(map2)

print(map1)
80 chars
7 lines

Output:

main.py
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
33 chars
2 lines

In this example, we first create two dictionaries map1 and map2. Then we use the update() method of map1 to merge map2 into map1. The resulting dictionary is printed to the console.

Note that if both maps contain the same keys, the values from the second map will overwrite the values in the first map during the merge.

gistlibby LogSnag