136

I have two dictionaries and I'd like to be able to make them one:

Something like this pseudo-Python would be nice:

dic0 = {'dic0': 0}
dic1 = {'dic1': 1}

ndic = dic0 + dic1
# ndic would equal {'dic0': 0, 'dic1': 1}
codeforester
  • 39,467
  • 16
  • 112
  • 140
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

6 Answers6

210

If you're interested in creating a new dict without using intermediary storage: (this is faster, and in my opinion, cleaner than using dict.items())

dic2 = dict(dic0, **dic1)

Or if you're happy to use one of the existing dicts:

dic0.update(dic1)
Neuron
  • 5,141
  • 5
  • 38
  • 59
bluepnume
  • 16,460
  • 8
  • 38
  • 48
  • 19
    good answer, but so non-intuitive. i wish '+' was implemented... – Berry Tsakala Nov 23 '14 at 11:16
  • 3
    Not very reliable: http://stackoverflow.com/a/2799082/1959808 – 0 _ Apr 11 '15 at 04:04
  • 9
    The first line fails if the keys are anything other than strings. – Flimm Mar 14 '16 at 15:49
  • 2
    @BerryTsakala The problem with `+` is what happens in case of conflicts ? `.update()` is properly asymmetric. – Nikana Reklawyks Dec 12 '17 at 07:33
  • You can make it terser by replacing 1st example with `dic2 = {dic0, **dic1}` – Hi-Angel May 26 '20 at 14:51
  • @NikanaReklawyks - Could either throw an exception (like `1/0` does) or just accept that it's asymmetric. Other implemented additions of non-numbers are already asymmetric - `[1] + [2]` is different from `[2] + [1]` and `'1' + '2'` is different from `'2' + '1'`. – ArtOfWarfare Sep 06 '20 at 02:30
  • 7
    @BerryTsakala you got your wish with python 3.9 :) But the operator is `|`, not `+`. https://docs.python.org/3/whatsnew/3.9.html – Nate Glenn Oct 18 '20 at 12:07
46

Here are quite a few ways to add dictionaries.

You can use Python3's dictionary unpacking feature:

ndic = {**dic0, **dic1}

Note that in the case of duplicates, values from later arguments are used. This is also the case for the other examples listed here.


Or create a new dict by adding both items.

ndic = dict(tuple(dic0.items()) + tuple(dic1.items()))

If modifying dic0 is OK:

dic0.update(dic1)

If modifying dic0 is NOT OK:

ndic = dic0.copy()
ndic.update(dic1)

If all the keys in one dict are ensured to be strings (dic1 in this case, of course args can be swapped)

ndic = dict(dic0, **dic1)

In some cases it may be handy to use dict comprehensions (Python 2.7 or newer),
Especially if you want to filter out or transform some keys/values at the same time.

ndic = {k: v for d in (dic0, dic1) for k, v in d.items()}
ideasman42
  • 42,413
  • 44
  • 197
  • 320
18
>>> dic0 = {'dic0':0}
>>> dic1 = {'dic1':1}
>>> ndic = dict(list(dic0.items()) + list(dic1.items()))
>>> ndic
{'dic0': 0, 'dic1': 1}
>>>
ideasman42
  • 42,413
  • 44
  • 197
  • 320
Vijay
  • 924
  • 7
  • 14
  • 2
    Note that the equivalent syntax for this in Python 3.x is `ndic = list(dict(dic0.items()) + list(dic1.items()))` since `.items()` not longer returns a list, but a (iterable)view – Bryce Guinta Jul 10 '15 at 16:54
  • 3
    @BryceGuinta I assume you meant `ndic = dict(list(dic0.items()) + list(dic1.items()))` – dimo414 Jul 18 '16 at 04:44
  • 1
    @dimo414 Yes, my bad. I can't change it now however. I use [ChainMap](https://docs.python.org/3/library/collections.html#collections.ChainMap) from `collections` to achieve this functionality now via `dict(ChainMap(dic1, dic0))`. However I have had to grab the source code from [the chainmap pypi package](https://pypi.python.org/pypi/chainmap/1.0.2) for Python2.7. Notice how I switched the order of the dicts. In the Vijay's example the rightmost keys' values overwrite the leftmost while ChainMap gets it right and the leftmost keys' values have precedence over the right. – Bryce Guinta Jul 28 '16 at 22:33
13

You are looking for the update method

dic0.update( dic1 )
print( dic0 ) 

gives

{'dic0': 0, 'dic1': 1}
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • 8
    Nice, although it modifies dic0. I'm not sure if that is acceptable to the original poster. – mseery May 14 '11 at 22:35
7
dic0.update(dic1)

Note this doesn't actually return the combined dictionary, it just mutates dic0.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
5

The easiest way to do it is to simply use your example code, but using the items() member of each dictionary. So, the code would be:

dic0 = {'dic0': 0}
dic1 = {'dic1': 1}
dic2 = dict(dic0.items() + dic1.items())

I tested this in IDLE and it works fine. However, the previous question on this topic states that this method is slow and chews up memory. There are several other ways recommended there, so please see that if memory usage is important.

wovano
  • 4,543
  • 5
  • 22
  • 49
LukeFitz
  • 156
  • 3