How to merge two dictionaries in a single expression?

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


How can I merge two Python dictionaries in a single expression?

For dictionaries x and y , z becomes a merged dictionary with values from y replacing those from x .

  • In Python 3.5 or greater, :

    z = {**x, **y}
    
  • In Python 2, (or 3.4 or lower) write a function:

    def merge_two_dicts(x, y):
        z = x.copy()   # start with x's keys and values
        z.update(y)    # modifies z with y's keys and values & returns None
        return z
    

    and

    z = merge_two_dicts(x, y)
    
  • Explanation

    Say you have two dicts and you want to merge them into a new dict without altering the original dicts:

    x = {'a': 1, 'b': 2}
    y = {'b': 3, 'c': 4}
    

    The desired result is to get a new dictionary ( z ) with the values merged, and the second dict's values overwriting those from the first.

    >>> z
    {'a': 1, 'b': 3, 'c': 4}
    

    A new syntax for this, proposed in PEP 448 and available as of Python 3.5, is

    z = {**x, **y}
    

    And it is indeed a single expression. It is now showing as implemented in the release schedule for 3.5, PEP 478, and it has now made its way into 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 backwards 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:

    z = x.copy()
    z.update(y) # which returns None since it mutates z
    

    In both approaches, y will come second and its values will replace x 's values, thus 'b' will point to 3 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 correct approach is to put it in a function:

    def merge_two_dicts(x, y):
        """Given two dicts, merge them into a new dict as a shallow copy."""
        z = x.copy()
        z.update(y)
        return z
    

    and then you have a single expression:

    z = merge_two_dicts(x, y)
    

    You can also make a function to merge an undefined number of dicts, from zero to a very large number:

    def merge_dicts(*dict_args):
        """
        Given any number of dicts, shallow copy and merge into a new dict,
        precedence goes to key value pairs in latter dicts.
        """
        result = {}
        for dictionary in dict_args:
            result.update(dictionary)
        return result
    

    This function will work in Python 2 and 3 for all dicts. eg given dicts a to g :

    z = merge_dicts(a, b, c, d, e, f, g) 
    

    and key value pairs in g will take precedence over dicts a to f , and so on.

    Critiques of Other Answers

    Don't use what you see in the formerly accepted answer:

    z = dict(x.items() + y.items())
    

    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 -

    >>> c = dict(a.items() + b.items())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
    

    and you would have to explicitly create them as lists, eg 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:

    >>> c = dict(a.items() | b.items())
    

    This example demonstrates what happens when values are unhashable:

    >>> x = {'a': []}
    >>> y = {'b': []}
    >>> dict(x.items() | y.items())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unhashable type: 'list'
    

    Here's an example where y should have precedence, but instead the value from x is retained due to the arbitrary order of sets:

    >>> x = {'a': 2}
    >>> y = {'a': 1}
    >>> dict(x.items() | y.items())
    {'a': 2}
    

    Another hack you should not use:

    z = dict(x, **y)
    

    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.

    Dicts are intended to take hashable keys (eg frozensets or tuples), but this method fails in Python 3 when keys are not strings.

    >>> c = dict(a, **b)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: keyword arguments must be strings
    

    From the mailing list, Guido van Rossum, the creator of the language, wrote:

    I am fine with declaring dict({}, **{1:3}) illegal, since after all it is abuse of the ** mechanism.

    and

    Apparently dict(x, **y) is going around as "cool hack" for "call x.update(y) and return x". Personally I find it more despicable than cool.

    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 dicts for readability purposes, eg:

    dict(a=1, b=10, c=11)
    

    instead of

    {'a': 1, 'b': 10, 'c': 11}
    

    Response to comments

    Despite what Guido says, dict(x, **y) is in line with the dict specification, which btw. works for both Python 2 and 3. The fact that this only works for string keys is a direct consequence of how keyword parameters work and not a short-comming of dict. Nor is using the ** operator in this place an abuse of the mechanism, in fact ** was designed precisely to pass dicts as keywords.

    Again, it doesn't work for 3 when keys are non-strings. The implicit calling contract is that namespaces take ordinary dicts, while users must only pass keyword arguments that are strings. All other callables enforced it. dict broke this consistency in Python 2:

    >>> foo(**{('a', 'b'): None})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: foo() keywords must be strings
    >>> dict(**{('a', 'b'): None})
    {('a', 'b'): None}
    

    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.

    Another comment:

    dict(x.items() + y.items()) is still the most readable solution for Python 2. Readability counts.

    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.

    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 and update 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 dicts have precedence)

    You can also chain the dicts manually inside a dict comprehension:

    {k: v for d in dicts for k, v in d.items()} # iteritems in Python 2.7
    

    or in python 2.6 (and perhaps as early as 2.4 when generator expressions were introduced):

    dict((k, v) for d in dicts for k, v in d.items())
    

    itertools.chain will chain the iterators over the key-value pairs in the correct order:

    import itertools
    z = dict(itertools.chain(x.iteritems(), y.iteritems()))
    

    Performance Analysis

    I'm only going to do the performance analysis of the usages known to behave correctly.

    import timeit
    

    The following is done on Ubuntu 14.04

    In Python 2.7 (system Python):

    >>> min(timeit.repeat(lambda: merge_two_dicts(x, y)))
    0.5726828575134277
    >>> min(timeit.repeat(lambda: {k: v for d in (x, y) for k, v in d.items()} ))
    1.163769006729126
    >>> min(timeit.repeat(lambda: dict(itertools.chain(x.iteritems(), y.iteritems()))))
    1.1614501476287842
    >>> min(timeit.repeat(lambda: dict((k, v) for d in (x, y) for k, v in d.items())))
    2.2345519065856934
    

    In Python 3.5 (deadsnakes PPA):

    >>> min(timeit.repeat(lambda: {**x, **y}))
    0.4094954460160807
    >>> min(timeit.repeat(lambda: merge_two_dicts(x, y)))
    0.7881555100320838
    >>> min(timeit.repeat(lambda: {k: v for d in (x, y) for k, v in d.items()} ))
    1.4525277839857154
    >>> min(timeit.repeat(lambda: dict(itertools.chain(x.items(), y.items()))))
    2.3143140770262107
    >>> min(timeit.repeat(lambda: dict((k, v) for d in (x, y) for k, v in d.items())))
    3.2069112799945287
    

    Resources on Dictionaries

  • My explanation of Python's dictionary implementation , updated for 3.6.
  • Answer on how to add new keys to a dictionary
  • Mapping two lists into a dictionary
  • The official Python docs on dictionaries
  • The Dictionary Even Mightier - talk by Brandon Rhodes at Pycon 2017
  • Modern Python Dictionaries, A Confluence of Great Ideas - talk by Raymond Hettinger at Pycon 2017

  • In your case, what you can do is:

    z = dict(x.items() + y.items())
    

    This will, as you want it, put the final dict in z , and make the value for key b be properly overridden by the second ( y ) dict's value:

    >>> x = {'a':1, 'b': 2}
    >>> y = {'b':10, 'c': 11}
    >>> z = dict(x.items() + y.items())
    >>> z
    {'a': 1, 'c': 11, 'b': 10}
    

    If you use Python 3, it is only a little more complicated. To create z :

    >>> z = dict(list(x.items()) + list(y.items()))
    >>> z
    {'a': 1, 'c': 11, 'b': 10}
    

    替代:

    z = x.copy()
    z.update(y)
    
    链接地址: http://www.djcxy.com/p/362.html

    上一篇: 我怎样才能在JavaScript中获取查询字符串值?

    下一篇: 如何在单个表达式中合并两个字典?