Combine two dictionary...

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Fri Oct 5 01:04:21 EDT 2007


In message <1191261690.056215.179920 at 57g2000hsv.googlegroups.com>, Abandoned
wrote:

> I want to total score..
> For example
>> > dict1={1: 4,  3: 5}... and 2 millions element
>> > dict2={3: 3,  8: 6}... and 3 millions element
> 
> result should be dict3={1:4, 3:8, 8:6}

Don't people like one-liners? :)

    dict((k, dict1.get(k, 0) + dict2.get(k, 0))
        for k in dict1.keys() + dict2.keys())



More information about the Python-list mailing list