[Python-ideas] Dict joining using + and +=
mertz at gnosis.cx
Mon Mar 4 13:43:12 EST 2019
On Mon, Mar 4, 2019, 11:45 AM Steven D'Aprano <steve at pearwood.info> wrote:
> > Like other folks in the thread, I also want to merge dicts three times
> > year.
> I'm impressed that you have counted it with that level of accuracy. Is it
> on the same three days each year, or do they move about? *wink*
To be respectful, I always merge dicts on Eid al-Fitr, Diwali, and Lent. I
was speaking approximate since those do not appears line up with the same
> And every one of those times, itertools.ChainMap is the right way to do
> that non-destructively, and without copying.
> Can you elaborate on why ChainMap is the right way to merge multiple dicts
> into a single, new dict?
> ChainMap also seems to implement the opposite behaviour to that usually
> desired: first value seen wins, instead of last:
True, the semantics are different, but equivalent, to the proposed dict
addition. I put the key I want to "win" first rather than last.
If you know ahead of time which order you want, you can simply reverse it:
This seems nonsensical. If I write, at some future time,
'dict1+dict2+dict3' I need exactly as much to know "ahead of time" which
keys I intend to win.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas