Dictionaries

Rob De Almeida ralmeida at gmail.com
Wed Oct 18 18:33:12 CEST 2006


Lad wrote:
> Let's suppose I have
>
>  a={'c':1,'d':2}
>  b={'c':2}
> but
>  a.update(b)
> will make
> {'c': 2, 'd': 2}
>
> and I would need
> {'c': 3, 'd': 2}
>
> (because `c` is 1 in `a` dictionary and `c` is 2 in `b` dictionary, so
> 1+2=3)
>
> How can be done that?

dict([(k, a.get(k, 0) + b.get(k,0)) for k in set(a.keys() + b.keys())])




More information about the Python-list mailing list