You should never use indexes into keys of dictionarys - especially not if you use python 2.x (as suggested by your use of print
without any ()
).
The order of keys in dictionarys is not fixed up until 3.6 (only for CPython as sideeffect of an implementation detail) or from python 3.7 on by default.
The order is then insert order - if you insert keys in different order, indexing into keys()
will still break because you are mixing values of different keys!.
rcmd = {'1':{"A","B"},"2":{"A","C"},"3":{"B","C","D"}}
rmv = {'1':{"C","B"},"2":{"A","C"},"3":{"B","C","A"},"4":{"A"}}
# get all keys that are common in both dictionaries
# python 2 or 3:
keys_that_are_same = set(rcmd) & set(rmv)
# for python 3 better, as it uses the key-sets directly:
# keys_that_are_same = rcmd.keys() & rmv.keys()
# loop over both keys and get the intersection into a new dict:
common = {}
for key in keys_that_are_same:
common[key] = rcmd[key] & rmv[key]
# as dict comprehension - no loop needed:
# common = {k : rcmd[k] & rmv[k] for k in keys_that_are_same}
print(common)
Output:
{'1': set(['B']), '3': set(['C', 'B']), '2': set(['A', 'C'])} # py 2.7
{'1': {'B'}, '2': {'A', 'C'}, '3': {'C', 'B'}} # py 3.6
This also has the benefit of using less objects that need to be constructed and handled as well as using sets to reduce the amount of keys to handle to begin with. If you are on python 2.