Issue 36431: Use dict unpacking for merging two dicts (original) (raw)

Issue36431

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:13 admin set github: 80612
2019-03-27 06:15:07 serhiy.storchaka set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2019-03-27 06:02:32 serhiy.storchaka set messages: +
2019-03-27 00:16:13 terry.reedy set nosy: + terry.reedymessages: +
2019-03-26 06:36:43 serhiy.storchaka set nosy: + rhettinger
2019-03-26 06:35:13 serhiy.storchaka set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest12500>
2019-03-26 06:33:08 serhiy.storchaka create