I am programming a development and I want to compare 2 dictionaries, which in common have keys of the same name.
For example dictionary 1 is of the form:
dic1 = {'0': [2.4636363650000002], '6': [4.1666666650000002], '9': [4.8333333349999998], '11': [3.5000000090000012], '14': [6.6181818249999989]}
Dictionary 2 is of the form:
dic2 = {'0': [2, 3, 4], '6': [5,6,7], '19': [4.8333333349999998], '10': [4.8333333349999998], '12': [3.5012]}
Seeing, dictionary 2 matches the 1 keys '0' and '6'.
My idea is to create a dictionary 3 containing only this:
dic3 = {'0': [2, 3, 4], '6': [5,6,7]}
What I am doing is creating lists with the keys of each dictionary, then I compare these lists and in the new dictionary I add those elements of the keys that match. I want to know if you know of a faster and more efficient way than looping through and creating lists, since there are so many keys I have.
There is a very simple method using "dictionary compressions" :
With a for loop you can get the key of each item in the dictionary and iterate through it.