By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

Dictionaries

P: n/a
Lad
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?

Thank you
L.

Oct 18 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On 18 Oct 2006 08:24:27 -0700, Lad <py****@hope.czwrote:
How can I add two dictionaries into one?
E.g.
a={'a:1}
b={'b':2}

I need

the result {'a':1,'b':2}.
>>a={'a':1}
b={'b':2}
a.update(b)
a
{'a': 1, 'b': 2}

--
Cheers,
Simon B
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Oct 18 '06 #2

P: n/a
Lad wrote:
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?

Thank you
L.

Yes, use update. Beware that this modifies a dictionary in place rather
than returning a new dictionary.
>>a={'a':1}
b={'b':2}
a.update(b)
a
{'a': 1, 'b': 2}

Gary Herron
Oct 18 '06 #3

P: n/a
How can I add two dictionaries into one?
E.g.
a={'a:1}
b={'b':2}

I need

the result {'a':1,'b':2}.
>>a.update(b)
a
{'a':1,'b':2}

-tkc
Oct 18 '06 #4

P: n/a

dict(a.items() + b.items())

Lad wrote:
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?
Oct 18 '06 #5

P: n/a
On Wed, 18 Oct 2006 08:24:27 -0700, Lad wrote:
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("duplicate key '%s' detected!" % key)
C[key] = value
return C
--
Steven.

Oct 18 '06 #6

P: n/a
Lad

Tim Chase wrote:
How can I add two dictionaries into one?
E.g.
a={'a:1}
b={'b':2}

I need

the result {'a':1,'b':2}.
>>a.update(b)
>>a
{'a':1,'b':2}

-tkc
Thank you ALL for help.
However It does not work as I would need.
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?
Thank you for the reply
L

Oct 18 '06 #7

P: n/a
However It does not work as I would need.
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}
Ah...a previously omitted detail.

There are likely a multitude of ways to do it. However, the one
that occurs to me off the top of my head would be something like

dict((k, a.get(k, 0) + b.get(k, 0)) for k in a.keys()+b.keys())
If the sets are huge, you can use itertools
>>from itertools import chain
dict((k, a.get(k, 0) + b.get(k, 0)) for k in
chain(a.keys(),b.keys()))
{'c': 3, 'd': 2}

which would reduce the need for creating the combined dictionary,
only to throw it away.

-tkc

Oct 18 '06 #8

P: n/a
Lad

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?
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("duplicate key '%s' detected!" % key)
C[key] = value
return C
--
Steven.
Oct 18 '06 #9

P: n/a
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())])

Oct 18 '06 #10

P: n/a
Lad wrote:
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>

Oct 18 '06 #11

P: n/a
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("duplicate 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.

Oct 18 '06 #12

P: n/a
Fredrik Lundh wrote:
Lad wrote:

>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.

Gary Herron

Oct 18 '06 #13

P: n/a
Lad
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("duplicate 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.
Oct 19 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.