Reputation: 101
I have two dictionaries:
sampleOne= {'a':4, 'b':7, 'c':3, 'd':5}
sampleTwo= {'a':6, 'b':3, 'c':7, 'd':2}
i have tried using:
for key,value in sampleOne:
print(key,value)
for key,value in samepleTwo:
print(key,value)
But this method is not working, I want the result to look like this:
a 4 6
b 7 3
c 3 7
d 5 2
However mine is looking like this:
a 4
b 7
c 3
d 5
a 6
b 3
c 7
d 2
Would appreciate the help. Thanks!
Upvotes: 0
Views: 85
Reputation: 133554
>>> sampleOne= {'a':4, 'b':7, 'c':3, 'd':5}
>>> sampleTwo= {'a':6, 'b':3, 'c':7, 'd':2}
>>> for k in sampleOne.viewkeys() | sampleTwo.viewkeys(): # On Py 3 use .keys() instead
print k, sampleOne.get(k, 0), sampleTwo.get(k, 0)
a 4 6
c 3 7
b 7 3
d 5 2
If you need the letters in order change this line:
for k in sampleOne.viewkeys() | sampleTwo.viewkeys()
to
for k in sorted(sampleOne.viewkeys() | sampleTwo.viewkeys())
Upvotes: 3