I want to merge two dictionaries into a new dictionary.
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = merge(x, y)
>>> z
{'a': 1, 'b': 3, 'c': 4}
Whenever a key k
is present in both dictionaries, only the value y[k]
should be kept.
dictionarymergepython
I want to merge two dictionaries into a new dictionary.
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = merge(x, y)
>>> z
{'a': 1, 'b': 3, 'c': 4}
Whenever a key k
is present in both dictionaries, only the value y[k]
should be kept.
Best Answer
How can I merge two Python dictionaries in a single expression?
For dictionaries
x
andy
, their shallowly-merged dictionaryz
takes values fromy
, replacing those fromx
.In Python 3.9.0 or greater (released 17 October 2020,
PEP-584
, discussed here):In Python 3.5 or greater:
In Python 2, (or 3.4 or lower) write a function:
and now:
Explanation
Say you have two dictionaries and you want to merge them into a new dictionary without altering the original dictionaries:
The desired result is to get a new dictionary (
z
) with the values merged, and the second dictionary's values overwriting those from the first.A new syntax for this, proposed in PEP 448 and available as of Python 3.5, is
And it is indeed a single expression.
Note that we can merge in with literal notation as well:
and now:
It is now showing as implemented in the release schedule for 3.5, PEP 478, and it has now made its way into the What's New in Python 3.5 document.
However, since many organizations are still on Python 2, you may wish to do this in a backward-compatible way. The classically Pythonic way, available in Python 2 and Python 3.0-3.4, is to do this as a two-step process:
In both approaches,
y
will come second and its values will replacex
's values, thusb
will point to3
in our final result.Not yet on Python 3.5, but want a single expression
If you are not yet on Python 3.5 or need to write backward-compatible code, and you want this in a single expression, the most performant while the correct approach is to put it in a function:
and then you have a single expression:
You can also make a function to merge an arbitrary number of dictionaries, from zero to a very large number:
This function will work in Python 2 and 3 for all dictionaries. e.g. given dictionaries
a
tog
:and key-value pairs in
g
will take precedence over dictionariesa
tof
, and so on.Critiques of Other Answers
Don't use what you see in the formerly accepted answer:
In Python 2, you create two lists in memory for each dict, create a third list in memory with length equal to the length of the first two put together, and then discard all three lists to create the dict. In Python 3, this will fail because you're adding two
dict_items
objects together, not two lists -and you would have to explicitly create them as lists, e.g.
z = dict(list(x.items()) + list(y.items()))
. This is a waste of resources and computation power.Similarly, taking the union of
items()
in Python 3 (viewitems()
in Python 2.7) will also fail when values are unhashable objects (like lists, for example). Even if your values are hashable, since sets are semantically unordered, the behavior is undefined in regards to precedence. So don't do this:This example demonstrates what happens when values are unhashable:
Here's an example where
y
should have precedence, but instead the value fromx
is retained due to the arbitrary order of sets:Another hack you should not use:
This uses the
dict
constructor and is very fast and memory-efficient (even slightly more so than our two-step process) but unless you know precisely what is happening here (that is, the second dict is being passed as keyword arguments to the dict constructor), it's difficult to read, it's not the intended usage, and so it is not Pythonic.Here's an example of the usage being remediated in django.
Dictionaries are intended to take hashable keys (e.g.
frozenset
s or tuples), but this method fails in Python 3 when keys are not strings.From the mailing list, Guido van Rossum, the creator of the language, wrote:
and
It is my understanding (as well as the understanding of the creator of the language) that the intended usage for
dict(**y)
is for creating dictionaries for readability purposes, e.g.:instead of
Response to comments
Again, it doesn't work for 3 when keys are not strings. The implicit calling contract is that namespaces take ordinary dictionaries, while users must only pass keyword arguments that are strings. All other callables enforced it.
dict
broke this consistency in Python 2:This inconsistency was bad given other implementations of Python (PyPy, Jython, IronPython). Thus it was fixed in Python 3, as this usage could be a breaking change.
I submit to you that it is malicious incompetence to intentionally write code that only works in one version of a language or that only works given certain arbitrary constraints.
More comments:
My response:
merge_two_dicts(x, y)
actually seems much clearer to me, if we're actually concerned about readability. And it is not forward compatible, as Python 2 is increasingly deprecated.Yes. I must refer you back to the question, which is asking for a shallow merge of two dictionaries, with the first's values being overwritten by the second's - in a single expression.
Assuming two dictionaries of dictionaries, one might recursively merge them in a single function, but you should be careful not to modify the dictionaries from either source, and the surest way to avoid that is to make a copy when assigning values. As keys must be hashable and are usually therefore immutable, it is pointless to copy them:
Usage:
Coming up with contingencies for other value types is far beyond the scope of this question, so I will point you at my answer to the canonical question on a "Dictionaries of dictionaries merge".
Less Performant But Correct Ad-hocs
These approaches are less performant, but they will provide correct behavior. They will be much less performant than
copy
andupdate
or the new unpacking because they iterate through each key-value pair at a higher level of abstraction, but they do respect the order of precedence (latter dictionaries have precedence)You can also chain the dictionaries manually inside a dict comprehension:
or in Python 2.6 (and perhaps as early as 2.4 when generator expressions were introduced):
itertools.chain
will chain the iterators over the key-value pairs in the correct order:Performance Analysis
I'm only going to do the performance analysis of the usages known to behave correctly. (Self-contained so you can copy and paste yourself.)
In Python 3.8.1, NixOS:
Resources on Dictionaries