How can I add two dictionaries into one?
E.g.
a={'a:1}
b={'b':2}
>
I need
>
the result {'a':1,'b':2}.
>
Is it possible?
What should the result be if both dictionaries have the same key?
a={'a':1, 'b'=2}
b={'b':3}
should the result be:
{'a':1, 'b'=2} # keep the existing value
{'a':1, 'b'=3} # replace the existing value
{'a':1, 'b'=[2, 3]} # keep both values
or something else?
Other people have already suggested using the update() method. If you want
more control, you can do something like this:
def add_dict(A, B):
"""Add dictionaries A and B and return a new dictionary."""
C = A.copy() # start with a copy of A
for key, value in B.items():
if C.has_key(key):
raise ValueError("dup licate key '%s' detected!" % key)
C[key] = value
return C
>
What should the result be if both dictionaries have the same key?
The answer: the values should be added together and assigned to the key
That is
{'a':1, 'b':5}
( from your example below)
Is there a solution?
Thanks for the reply
L.
>
a={'a':1, 'b'=2}
b={'b':3}
>
should the result be:
{'a':1, 'b'=2} # keep the existing value
{'a':1, 'b'=3} # replace the existing value
{'a':1, 'b'=[2, 3]} # keep both values
or something else?
>
Other people have already suggested using the update() method. If you want
more control, you can do something like this:
>
def add_dict(A, B):
"""Add dictionaries A and B and return a new dictionary."""
C = A.copy() # start with a copy of A
for key, value in B.items():
if C.has_key(key):
raise ValueError("dup licate key '%s' detected!" % key)
C[key] = value
return C
>
>
--
Steven.
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())])
>
Steven,
Thank you for your reply and question.
>
>>
>What should the result be if both dictionaries have the same key?
The answer: the values should be added together and assigned to the key
That is
{'a':1, 'b':5}
( from your example below)
>
Is there a solution?
Of course there is a solution. You just have to program it.
Look again at my example code before:
def add_dict(A, B):
"""Add dictionaries A and B and return a new dictionary."""
C = A.copy() # start with a copy of A
for key, value in B.items():
if C.has_key(key):
raise ValueError("dup licate key '%s' detected!" % key)
C[key] = value
return C
Can you see how to modify this function to do what you want?
(Hint: instead of raising a ValueError exception, you want to do something
else.)
>The answer: the values should be added together and assigned to the key
>That is
>{'a':1, 'b':5}
>( from your example below)
>>
>Is there a solution?
>>
>
have you tried coding a solution and failed, or are you just expecting
people to code for free?
>
</F>
>
>
Right. This thread begins to look like someone with a homework
assignment asking for code rather than attempting to try programming it
up himself. I was happy to answer the first plea for help, but I'd be
leery of providing any actual code.
Steven,
Thank you for help;
Here is a code that works in a way I need
A={'c':1,'d':2, 'e':3,'f':2}
B={'c':2,'e':1}
if len(A)>=len(B):
Delsi=B
C = A.copy()
else:
Delsi=A
C = B.copy()
for key, value in Delsi.items():
if C.has_key(key):
C[key]=C[key]+value
else:
C[key]=value
How easy :-)
Regards,
L.
Steven D'Aprano wrote:
On Wed, 18 Oct 2006 09:31:50 -0700, Lad wrote:
>
Steven,
Thank you for your reply and question.
>
What should the result be if both dictionaries have the same key?
The answer: the values should be added together and assigned to the key
That is
{'a':1, 'b':5}
( from your example below)
Is there a solution?
>
Of course there is a solution. You just have to program it.
>
Look again at my example code before:
>
def add_dict(A, B):
"""Add dictionaries A and B and return a new dictionary."""
C = A.copy() # start with a copy of A
for key, value in B.items():
if C.has_key(key):
raise ValueError("dup licate key '%s' detected!" % key)
C[key] = value
return C
>
>
Can you see how to modify this function to do what you want?
>
(Hint: instead of raising a ValueError exception, you want to do something
else.)
>
>
--
Steven.
Comment