[Python-ideas] Adding "+" and "+=" operators to dict
Joshua Landau
joshua at landau.ws
Sat Feb 14 00:32:14 CET 2015
On 13 February 2015 at 02:24, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, Feb 13, 2015 at 12:06:44AM +0000, Andrew Barnert wrote:
>> On Thursday, February 12, 2015 1:33 PM, Nathan Schneider <neatnate at gmail.com> wrote:
>>
>> > A reminder about PEP 448: Additional Unpacking Generalizations
>> > (https://www.python.org/dev/peps/pep-0448/), which claims that "it
>> > vastly simplifies types of 'addition' such as combining
>> > dictionaries, and does so in an unambiguous and well-defined way".
>> > The spelling for combining two dicts would be: {**d1, **d2}
>
> Very nice! That should be extensible to multiple arguments without the
> pathologically slow performance of repeated addition:
>
> {**d1, **d2, **d3, **d4}
>
> and you can select which dict you want to win in the event of clashes
> by changing the order.
Better than that, you can even do
{a: b, **d1, c: d, **d2, e: f, <...etc>}
Best of all, it's implemented (albeit not yet accepted or reviewed):
http://bugs.python.org/issue2292.
More information about the Python-ideas
mailing list