First page Back Continue Last page Overview Graphics

Dictionaries concatenating

Method update() can be used to append a dictionary into another dictionary. If the second dictionary contains keys, which are contained in the first one as well, they get reassigned with the values of the second dictionary.

>>> d1 = {'a': 232, 'c': 543, 'b': 343, 'd': 23}

>>> d2 = {'c': 299, 'e': 543, 'd': 399, 'f': 23}

>>> d1.update(d2)

>>> d1

{'a': 232, 'c': 299, 'b': 343, 'e': 543, 'd': 399, 'f': 23}

>>>

>>> d1 = {'a': 232, 'c': 543, 'b': 343, 'd': 23}

>>> d2 = {'c': 299, 'e': 543, 'd': 399, 'f': 23}

>>> d2.update(d1)

>>> d2

{'a': 232, 'c': 543, 'b': 343, 'e': 543, 'd': 23, 'f': 23}

>>> d1

{'a': 232, 'c': 543, 'b': 343, 'd': 23}