forum

Home / DeveloperSection / Forums / Merge two Python dictionaries in a single expression?

Merge two Python dictionaries in a single expression?

Anonymous User 2641 13-May-2015
I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update() method would be what I need, if it returned its result instead of modifying a dict in-place.
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = x.update(y)
>>> print z
None
>>> x
{'a': 1, 'b': 10, 'c': 11}
How can I get that final merged dict in z, not x?

(To be extra-clear, the last-one-wins conflict-handling of dict.update() is what I'm looking for as well.)

Updated on 13-May-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By