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

E. Madison Bray erik.m.bray at gmail.com
Wed Feb 27 12:37:01 EST 2019


On Wed, Feb 27, 2019 at 6:35 PM George Castillo <gmcastil at gmail.com> wrote:
>>
>> 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`.
>
>
> This would mean that addition, at least in this particular instance, is not a commutative operation.  Are there other places in Python where this is the case?

Sure:

>>> a = "A"
>>> b = "B"
>>> a + b == b + a
False

> On Wed, Feb 27, 2019 at 10:06 AM 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`.
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list