Set Operations on Dicts

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Feb 8 10:05:56 EST 2016


Random832 writes:

> On Mon, Feb 8, 2016, at 08:32, Matt Wheeler wrote:
>> On 8 February 2016 at 12:17, Jussi Piitulainen wrote:
>> > Also, what would be the nicest current way to express a priority union
>> > of dicts?
>> >
>> > { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }
>> 
>> Since Python 3.5: {**e, **d}
>
> And before that... dict(ChainMap(d, e))

New in version 3.3. Thanks, I didn't know of that either. Reading the
docs, I think for the use I had in mind, ChainMap(d, e) itself would
suffice.


More information about the Python-list mailing list