RottenLettuce
RottenLettuce

Reputation: 5

How to match up two dictionaries in Python

{u'Orange': [u'OR', u'LI', u'LE'], u'Red': [u'AP', u'ST']}
{u'Orange': [u'ORANGE', u'LIME', u'LEMON'], u'Red': [u'APPLE', u'STRAWBERRY']}

Beginning in Python and I am having some difficulties with dictionaries. I have these two dictionaries here.

The output I want is:

Orange: 

Category: Orange
OR - ORANGE
LI - LIME
LE - LEMON

Category: Red
AP - APPLE
ST - STRAWBERRY

Unsure how I would match up the two dictionaries. I could always put them into one dictionary and access every other value but I would like to keep them as two dictionaries. There will never be an instance where a two letter code does not have an associated value. The order is always set as well.

An idea I had is to create a third dictionary out of the two, but even then I'm unsure where to go.

Upvotes: 0

Views: 145

Answers (4)

Mr. E
Mr. E

Reputation: 2120

Depending on what you want to do after could use a dictionary or just a generator

d1 = {u'Orange': [u'OR', u'LI', u'LE'], u'Red': [u'AP', u'ST']}
d2 = {u'Orange': [u'ORANGE', u'LIME', u'LEMON'], u'Red': [u'APPLE', u'STRAWBERRY']}

new_dict_generator = (zip(d1[k], d2[k]) for k in d1) #Generator
new_dict = {k:zip(d1[k], d2[k]) for k in d1} #Dictionary comprehension

Then you can use it like

for pairs in new_dict_generator:
    print(pair)

The advantage of the generator is that you don't need to have the entire dictionary in memory, but you won't be able to access it by key:

>>> new_dict_generator['Orange']
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: 'generator' object has no attribute '__getitem__'

>>> new_dict['Orange']
[(OR, ORANGE), (LI, LIME), (LE, LEMON)]

Edit: If at some time both lists could have different size you may want to take a look at itertools.izip_longest

Upvotes: 0

zondo
zondo

Reputation: 20336

Try this:

dict1 = {u'Orange': [u'OR', u'LI', u'LE'], u'Red': [u'AP', u'ST']}
dict2 = {u'Orange': [u'ORANGE', u'LIME', u'LEMON'], u'Red': [u'APPLE', u'STRAWBERRY']}

for color in dict1.keys():
    print("Category: %s" % color)
    print("\n".join(" - ".join((x, y)) for x, y in zip(dict1[color], dict2[color])))
    print()

Don't I love list-comprehension.

Upvotes: 0

inspectorG4dget
inspectorG4dget

Reputation: 113965

d1 = {u'Orange': [u'OR', u'LI', u'LE'], u'Red': [u'AP', u'ST']}
d2 = {u'Orange': [u'ORANGE', u'LIME', u'LEMON'], u'Red': [u'APPLE', u'STRAWBERRY']}

for k,abbrevs in d1.items():
    print("Category:", k)
    for abb, name in zip(abbrevs, d2[k]):
        print("{} - {}".format(abbrev, name))

Upvotes: 0

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23223

All you have to do is to iterate over available keys, and zip lists retrieved from both dictionaries to iterate over pairs simultaneously.

Basic implementation may look like this:

d1 = {u'Orange': [u'OR', u'LI', u'LE'], u'Red': [u'AP', u'ST']}
d2 = {u'Orange': [u'ORANGE', u'LIME', u'LEMON'], u'Red': [u'APPLE', u'STRAWBERRY']}

for k in d1:
    print("Category:", k)
    for abbr, val in zip(d1[k], d2[k]):
        print(abbr, "-", val)
    print()

This prints on standard output following text:

Category: Orange
OR - ORANGE
LI - LIME
LE - LEMON

Category: Red
AP - APPLE
ST - STRAWBERRY

Obviously you'll have to handle some exceptional cases, like missing keys in dictionaries etc.

Upvotes: 3

Related Questions