[Python-ideas] Dict joining using + and +=

Oleg Broytman phd at phdru.name
Wed Feb 27 12:17:48 EST 2019


On Wed, Feb 27, 2019 at 09:05:20AM -0800, Guido van Rossum <guido at python.org> wrote:
> On Wed, Feb 27, 2019 at 8:50 AM Rhodri James <rhodri at kynesim.co.uk> wrote:
> 
> > On 27/02/2019 16:25, Jo??o Matos wrote:
> > > I would like to propose that instead of using this (applies to Py3.5 and
> > upwards)
> > > dict_a = {**dict_a, **dict_b}
> > >
> > > we could use
> > > dict_a = dict_a + dict_b
> > >
> > > or even better
> > > dict_a += dict_b
> >
> > While I don't object to the idea of concatenating dictionaries, I feel
> > obliged to point out that this last is currently spelled
> > dict_a.update(dict_b)
> >
> 
> This is likely to be controversial. But I like the idea. After all, we have
> `list.extend(x)` ~~ `list += x`. The key conundrum that needs to be solved
> is what to do for `d1 + d2` when there are overlapping keys. I propose to
> make d2 win in this case, which is what happens in `d1.update(d2)` anyways.
> If you want it the other way, simply write `d2 + d1`.

That is, ``d1 + d2`` is::

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

> -- 
> --Guido van Rossum (python.org/~guido)

Oleg.
-- 
    Oleg Broytman            https://phdru.name/            phd at phdru.name
           Programmers don't die, they just GOSUB without RETURN.


More information about the Python-ideas mailing list