Vince Vince - 1 year ago 66
Python Question

Comparing keys of one dictionary to another dictionary with a list of values

I have a dictionary with a list of values of varying numbers of items.
I want to compare the values of this dictionary (dict1) to the keys of another (dict2), and if they match print the key and value of the matching component of dict1, along with the value of dict2.
Both of the dictionaries are quite large, and currently this is taking way too long as you could have guessed from this basic script.

dict1 = {boys:[tom,jon],girls:[suzy]}

dict2 = {suzy:paper-stapler-extraordinaire,jon:paper-shredderoligist,tom:garbage-specialist}


boys \t tom \t garbage-specialist

boys \t jon \t paper-shredderoligist etc.....

for k,v in dict2.items():

for key,value in dict1.items():
if k in value[0]:
print str(key)+"\t"+str(value[0])+"\t"+v
if len(value)>1:
if k in value[1]:
print str(key)+"\t"+str(value[0])+"\t"+v

Could someone suggest a more memory efficient method? Perhaps a list comprehension?
This has not been working...
a = [k for k in dict2 if k in dict]

Answer Source
for dict1_key, dict1_values in dict1.iteritems():
    for dict1_value in dict1_values:
            dict2_value = dict2[dict1_value]
            print str(dict1_key) + '\t' + str(dict1_value) + '\t' + str(dict2_value)
        except KeyError:

That combines a few techniques to speed it up and use less memory. iteritems uses less memory (as others have mentioned). By using try and then using dict2[dict1_value], you can ensure that dict2 is only searched once, and then with the hash algorithm, which should be much faster than iterating through all the elements. For all the cases where dict2 does not have dict1_value in it, the first statement under try fails, causing the catch block to harmlessly pass.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download