Felipe Lira Felipe Lira - 1 year ago 102
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 Source
for k, v in d1.iteritems():
    if v in d2:
        print('{}\t{}\t{}'.format(k, v, d2[v]))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download