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

how to join two Dictionary together?

P: n/a

dict1={...something...}

dict2={...somethind else ..}

dict1 + dict2
that's does works ..:(, it's not like List...
anyone can tell me how to get it?

thanks in advance

Aug 30 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a

DENG wrote:
dict1={...something...}

dict2={...somethind else ..}

dict1 + dict2
that's does works ..:(, it's not like List...
anyone can tell me how to get it?


Uh, I'm just learning python too, so there may be a much simpler way to
do this, but you could:

dict3 = {}
for k,i in dict1.iteritems():
dict3[k] = i
for k,i in dict2.iteritems():
dict3[k] = i

Think this should work. Obviously, if the same key appears in both
dict1 and dict2 then dict2's value will overwrite dict1's.

Iain

Aug 30 '05 #2

P: n/a
DENG wrote:
dict1={...something...}

dict2={...somethind else ..}

dict1 + dict2
that's does works ..:(, it's not like List...


I think you want..

dict1.update(dict2)
Will McGugan
--
http://www.willmcgugan.com
"".join({'*':'@','^':'.'}.get(c,0) or chr(97+(ord(c)-84)%26) for c in
"jvyy*jvyyzpthtna^pbz")
Aug 30 '05 #3

P: n/a
yes, that's really what i want!

the 2nd replace the 1st one' value!

thanks so much King

Aug 30 '05 #4

P: n/a

Iain King wrote:
DENG wrote:
dict1={...something...}

dict2={...somethind else ..}

dict1 + dict2
that's does works ..:(, it's not like List...
anyone can tell me how to get it?


Uh, I'm just learning python too, so there may be a much simpler way to
do this, but you could:

dict3 = {}
for k,i in dict1.iteritems():
dict3[k] = i
for k,i in dict2.iteritems():
dict3[k] = i

Think this should work. Obviously, if the same key appears in both
dict1 and dict2 then dict2's value will overwrite dict1's.

Iain


Or, on reading the tutorial a bit more thoroughly, you can do:

dict1.update(dict2)

which will add all of dict2's key:value pairs to dict1

Iain

Aug 30 '05 #5

P: n/a
See http://docs.python.org/lib/typesmapping.html for a description of
the "update" method.
--
Benji York
Aug 30 '05 #6

P: n/a
DENG wrote:
dict1={...something...}
dict2={...somethind else ..}
dict1 + dict2

that's does works ..:(, it's not like List...
anyone can tell me how to get it?


I would do the following in most cases.
dict1={'a':'A'}
dict2={'b':'B'}
dict3=dict1.copy()
dict3 {'a': 'A'} dict3.update(dict2)
dict3

{'a': 'A', 'b': 'B'}

HTH
/Mikael Olofsson

Universitetslektor (Senior Lecturer [BrE], Associate Professor [AmE])
Linköpings universitet

-----------------------------------------------------------------------
E-Mail: mi****@isy.liu.se
WWW: http://www.dtr.isy.liu.se/en/staff/mikael
Phone: +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
-----------------------------------------------------------------------
Linköpings kammarkör: www.kammarkoren.com Vi söker tenorer och basar!
Aug 30 '05 #7

P: n/a
look Pyth Cookbook 2nd edit, Sec 4.17: "Unions/intersections
dictionaries". You'll see idioms like this for dict unions:

uniondict=dict(dict1, **dict2)
filter (dicta.has_key, dictb.keys())

DENG wrote:
yes, that's really what i want!

the 2nd replace the 1st one' value!

thanks so much King


Aug 30 '05 #8

P: n/a
In article <11*********************@g43g2000cwa.googlegroups. com>,
DENG <po***********@gmail.com> wrote:

dict1={...something...}

dict2={...somethind else ..}

dict1 + dict2
that's does works ..:(, it's not like List...
anyone can tell me how to get it?

thanks in advance


First, you have to tell us what you want to happen when the two
dictionaries have overlapping keys. Let's say I have:

d1 = {1: 'one', 2: 'two'}
d2 = {1: 'uno', 3: 'tres'}

if I do d3 = addDicts (d1, d2), what do you want d3 to have in it?
More specifically, what do you want the value of d3[1] to be?


Aug 30 '05 #9

P: n/a
In article <11*********************@g43g2000cwa.googlegroups. com>,
DENG <po***********@gmail.com> wrote:

dict1={...something...}
dict2={...somethind else ..}
dict1 + dict2


Another option to look into: if you're only using the dict keys and not
the valus, maybe you should use sets.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

The way to build large Python applications is to componentize and
loosely-couple the hell out of everything.
Aug 30 '05 #10

P: n/a
Read the list of dict methods at
http://docs.python.org/lib/typesmapping.html
Every Pythoneer should read LibRef c.2 at least once and either refer back
to it or use help() to refresh memory of available methods.

Terry J. Reedy

Sep 1 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.