439,931 Members | 1,976 Online 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 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
13 Replies

 P: n/a On 18 Oct 2006 08:24:27 -0700, Lad >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 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 chaindict((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 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? Oct 18 '06 #11 