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

Nagy László Zsolt gandalf at shopzeus.com
Tue Sep 27 03:08:25 EDT 2016


The result that I need should be a real dict, not just a ChainMap. (It
is because I have to mutate it.)

d1 = {'a':1, 'b':2}
d2 = {'c':3, 'd':4}
d3 = {'e':5, 'f':6}

#1. My first naive approach was:


from collections import ChainMap
d = {}
for key,value in ChainMap(d1, d2, d3).items():
    d[key] = value

#2. Much more effective version, but requires too many lines:

d= {}
d.update(d1)
d.update(d2)
d.update(d3)

#3. Third version is more compact. It uses a side effect inside a list
comp., so I don't like it either:

d = {}
[d.update(_) for _ in [d1, d2, d3]]


#4. Last version:

d = {}
d.update(ChainMap(d1, d2, d3))

Visually, it is the cleanest and the easiest to understand. However, it
uses ChainMap.__iter__ and that goes over all mappings in a loop written
in pure Python.

Is there a version that is as effective as #3, but as clean and nice as #4?





More information about the Python-list mailing list