Timeline for How do I merge two dictionaries in a single expression in Python?
Current License: CC BY-SA 3.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 16 at 17:30 | comment | added | phobie | @MurtazaMohsin for sure it "works", but will it do what you want? Think about which merge-strategy you need in your code! My answer just tries to offer a simple solution to the OPs question. For recursive updates you should have a look at Deep merge dictionaries of dictionaries in Python. | |
Oct 27, 2022 at 15:24 | comment | added | Murtaza Mohsin | This solution works but will it work if let's say one dict has a bunch of nested lists and the other is a simple dict with replacement values? | |
Jun 14, 2020 at 23:16 | comment | added | Corman | I suggest you put this into a function | |
May 6, 2013 at 11:50 | comment | added | phobie | Your suggestion would change this to Matthews answer. While his answer is fine, I think mine is more readable and better maintainable. The extra line would only be bad if it would cost execution time. | |
Mar 8, 2013 at 15:10 | comment | added | martineau |
Lose the z4 = {} and change the next line to z4 = x.copy() -- better than just good code doesn't do unnecessary things (which makes it even more readable and maintainable).
|
|
Oct 28, 2011 at 3:36 | comment | added | phobie | Yes! The mentioned one-expression-solutions are either slow or evil. Good code is readable and maintainable. So the problem is the question not the answer. We should ask for the best solution of a problem not for a one-line-solution. | |
Oct 18, 2011 at 15:44 | comment | added | fortran | but it's three statements rather than one expression | |
Oct 14, 2011 at 16:12 | history | answered | phobie | CC BY-SA 3.0 |