how to join two Dictionary together?

D

DENG

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
 
I

Iain King

DENG said:
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
 
W

Will McGugan

DENG said:
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
 
D

DENG

yes, that's really what i want!

the 2nd replace the 1st one' value!

thanks so much King
 
I

Iain King

Iain said:
DENG said:
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
 
M

Mikael Olofsson

DENG said:
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.
{'a': 'A', 'b': 'B'}

HTH
/Mikael Olofsson

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

gene tani

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())
 
R

Roy Smith

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?
 
A

Aahz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,318
Members
47,998
Latest member
GretaCjy4

Latest Threads

Top