Felipe Lira Felipe Lira - 10 days ago 6
Python Question

Concatenate dictionaries using values in different dictionaries

I have two dictionaries.

Like this:

d1 = {A: 1, B:2, C:3}
d2 = {1: x, 2: xx, 3: xxx, 4: xxxxx}


I wrote a code to compare both and print as a table like this:

A 1 xx
B 2 xxx
C 3 xxxx

for k, v in d1.iteritems():
for l, m in d2.iteritems():
if l == v:
print k+'\t'+v+'\t'+m


Suggestions like to create a new_dictionary is accepted.

I forgot to mention that d2 has more elements than d1.

Answer
for k, v in d1.iteritems():
    if v in d2:
        print('{}\t{}\t{}'.format(k, v, d2[v]))