If someone wonder about
If want to retaining values when there is key collisions use defaultdict.
**
,so is it PEP 448 and work for 3.5-->.wavic Wrote:Where 'two' gone?The task here was not to retaining values,as mention dictionary can not have duplicate keys.
If want to retaining values when there is key collisions use defaultdict.
>>> from itertools import chain ... from collections import defaultdict ... d1 = {1:'one',2:'two'} ... d2 = {2:'deux',3:'trois'} ... d3 = defaultdict(list) ... for k, v in chain(d1.items(), d2.items()): ... d3[k].append(v) >>> d3 defaultdict(<class 'list'>, {1: ['one'], 2: ['two', 'deux'], 3: ['trois']})