This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use dict unpacking for merging two dicts
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2019-03-26 06:33 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 12553 merged serhiy.storchaka, 2019-03-26 06:35
Messages (4)
msg338857 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-26 06:33
The following PR replaces the sequence of statement

    d = d1.copy()
    d.update(d2)

(where d1 and d2 are dicts) with a form proposed in PEP 448:

    d = {**d1, **d2}

or equivalent.

Besides functools, where using the new syntax makes the code clearer, there are not much occurrences of such idiom: only in yet 5 files, 1-2 times per file.
msg338927 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-03-27 00:16
3 years ago, Trey Hunter found 11 ways to merge to a new dict.
https://treyhunner.com/2016/02/how-to-merge-dictionaries-in-python/
He followed up with a performance comparison.
https://gist.github.com/treyhunner/f35292e676efa0be1728
** unpacking was nearly twice as fast as the 2nd place methods.
(Bigger dict might change the ratio, but I expect ** unpacking to remain first.)

Trey's summary: "This is simple and Pythonic. There are quite a few symbols, but it’s fairly clear that the output is a dictionary at least."  I consider the last point a major plus.

User comment: "Beautiful. Pythonic. Thank you."  No negatives.
msg338937 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-27 06:02
New changeset da0847048aa7f934573fa449cea8643def056aa5 by Serhiy Storchaka in branch 'master':
bpo-36431: Use PEP 448 dict unpacking for merging two dicts. (GH-12553)
https://github.com/python/cpython/commit/da0847048aa7f934573fa449cea8643def056aa5
msg338938 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-27 06:15
I hope the low number of such changes has clearly shown that we do not need the plus operator as yet one way of merging two dicts.
History
Date User Action Args
2022-04-11 14:59:13adminsetgithub: 80612
2019-03-27 06:15:07serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg338938

stage: patch review -> resolved
2019-03-27 06:02:32serhiy.storchakasetmessages: + msg338937
2019-03-27 00:16:13terry.reedysetnosy: + terry.reedy
messages: + msg338927
2019-03-26 06:36:43serhiy.storchakasetnosy: + rhettinger
2019-03-26 06:35:13serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request12500
2019-03-26 06:33:08serhiy.storchakacreate