How to make a copy of chained dicts effectively and nicely?

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Tue Sep 27 10:54:04 EDT 2016


MRAB writes:

> On 2016-09-27 08:32, Jussi Piitulainen wrote:
> [snip]
>
>> Is ChainMap really that bad? Otherwise the following would look somewhat
>> nice:
>>
>> d = dict(ChainMap(d1, d2, d3).items())
>>
>> Those come to mind.
>>
> You can copy a dict just by passing it to 'dict':
>
>     d = dict(d1)
>
> so I wondered if you can do the same with ChainMap:
>
>     d = dict(ChainMap(d1, d2, d3))
>
> Yep, you can!

I wasn't sure if it makes a copy or just returns the dict. But it's
true: help(dict) says dict(mapping) is a "new dictionary initialized
from a mapping object's (key, value) pairs".



More information about the Python-list mailing list