[Python-ideas] Dict joining using + and +=
Steven D'Aprano
steve at pearwood.info
Fri Mar 1 00:49:56 EST 2019
On Thu, Feb 28, 2019 at 08:59:30PM -0800, Hasan Diwan wrote:
> Do we really need a "+" and a "-" operation on dictionaries?
> [dictinstance.update({k:v}) for k,v in dictinstance.items()] does handle
> merges already.
I don;t think that does what you intended. That merges dictinstance with
itself (a no-op!), but one item at a time, so in the slowest, most
inefficient way possible.
Writing a comprehension for its side-effects is an anti-pattern that
should be avoided. You are creating a (potentially large) list of Nones
which has to be created, then garbage collected.
> And I'm assuming that "-" should return the difference --
> set(d1.keys()) - set(d2.keys()), right?
No. That throws away the values associated with the keys.
P.S. As per Guido's ~~command~~ request *wink* I'm writing a PEP for
this. I should have a draft ready later this evening.
--
Steven
More information about the Python-ideas
mailing list