+2 votes
in Programming Languages by (15.2k points)
How can I merge two dictionaries so that all the elements are in one dictionary? E.g. if x and y are two dictionaries, I want the elements of both x and y in x.

1 Answer

0 votes
by (25.6k points)

Use update function to merge the dictionaries. Check the following example.

>>> a1={'a':1,'b':2,'c':3}

>>> a2={'d':4,'e':5,'f':6}

>>> a1.update(a2)

>>> a1

{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}