+2 votes
in Programming Languages by (71.8k 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

+1 vote
by (349k points)
selected by
 
Best answer

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}


...