-3

I want to do something like this:

merge({'a': 1}, {'b': 2})

and get a return of {'a': 1, 'b':2}.

  1. I don't want to use .update(), which sadly doesn't return the updated or a new dict, just None (why?.....).
  2. Since I know .update(), I could write a 3 line function for this. I don't, I just want an already existing/known package that do this kind of job in a simple way.

EDIT

This edit is just to explain the lovely SO users that this is not a duplicate of How to merge two Python dictionaries in a single expression? and hence the chosen answer is unrelated with any of the answers provided in the candidate duplicate.

Community
  • 1
  • 1
oblitum
  • 11,380
  • 6
  • 54
  • 120
  • 3
    Possible duplicate of [How can I merge two Python dictionaries in a single expression?](http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression) – ShadowRanger Mar 01 '16 at 23:26
  • Not a duplicate. This one is asking for **packages that do this**, since it's not much native of python 2. Candidate duplicate is not looking for this aspect, and not providing answers in this sense. – oblitum Mar 01 '16 at 23:31
  • 3
    @pepper_chico. So your question is actually [off-topic](http://stackoverflow.com/help/on-topic), since you're asking people to recommend a software library. – ekhumoro Mar 01 '16 at 23:41
  • 2
    You want to introduce a dependency for *this*? – user2357112 Mar 01 '16 at 23:41
  • @user2357112 yes!!, this act make it clear how nice this language is. – oblitum Mar 01 '16 at 23:45
  • @ekhumoro if asking for a package that implements a given feature (not asking whether it's good or not and general recommendations) were to be deleted as off-topic this site would be bordering uselessness. – oblitum Mar 01 '16 at 23:57
  • @pepper_chico. The help topic I linked to is pretty clear about what's off topic and why (see item four). There's also a [software recommendations SE site](http://softwarerecs.stackexchange.com/) which may be more suitable for this kind of question. – ekhumoro Mar 02 '16 at 00:05
  • @ekhumoro didn't know of this SE site, first time seeing it. If I could, I'd migrate this question including answers there. Dunno whether SO (and SE in general) supports this. – oblitum Mar 02 '16 at 00:07
  • @ekhumoro If there's no real migrating besides asking again there, better to leave it here since it's already answered. – oblitum Mar 02 '16 at 00:08
  • @pepper_chico. Well, you seem to get the answer you were looking for, so it's probably best to just leave it here (but closed). – ekhumoro Mar 02 '16 at 00:10

3 Answers3

2

The package dictmerge can merge dictionaries.

As of Python 3.5 you can merge dictionaries without an additional package:

>>> d1 = {'a': 1}
>>> d2 = {'b': 2}
>>> {**d1, **d2}
{'b': 2, 'a': 1}
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • do you know whether this package doesn't suffer from slower perf like referred in http://stackoverflow.com/a/26853961/1000282? – oblitum Mar 01 '16 at 23:34
  • 1
    @pepper_chico: I haven't measured it but it doesn't make unnecessary copies (implementation is fairly short as well): https://github.com/nvie/dictmerge/blob/master/dictmerge.py – Simeon Visser Mar 01 '16 at 23:39
2

In Python 2.7, you can one-line (ignoring imports) a merge with:

from itertools import chain

merged = dict(chain(dict1.viewitems(), dict2.viewitems()))

In Py3.5 and higher, you can use additional unpacking generalizations to do this without any functions at all:

merged = {**dict1, **dict2}

The reason dict.update returns None is because Python methods, as a general rule, stick to either mutating an existing object or returning a new object, not both. Basically, functions with side-effects aren't supposed to be used in a functional manner (because it's liable to confuse).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • I realize the why, the "why" was more rethorical than truthful. IMO, this is just not to confuse newbies, at the expense of usability, hence ".......". – oblitum Mar 01 '16 at 23:38
0
d1 = {'a': 1}
d2 = {'b': 2}

d3 = dict(d1, **d2)

As ShadowRanger points out in the comments, this only works if all keys of d2 are of type str.

Alex
  • 18,484
  • 8
  • 60
  • 80