Home How to merge two dictionaries in a single expression?
Reply: 0

How to merge two dictionaries in a single expression?

user5342
1#
user5342 Published in September 21, 2018, 8:16 am

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.)

share|improve this question

49 Answers 49

active oldest votes
1 2 next
up vote 3497 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.502109 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO