I have a dictionary:
dict = {'A':[1,2,5],'B':[3,6,13],'C':[2,3,6],'D':[4,6,8]}
I want to extract all the common elements into a new dictionary as keys with their corresponding values as the keys in dict
which they are extracted from.
What I want is:
newdict = {1:['A'],2:['A','C'],3:['B','C'],4:['D'],5:['A'],6:['B','C','D'],8:['D'],13:['B']}
.
I have tried to compare values of each element by copying the dictionary dict
and comparing each of the elements (dict1
is copy of dict
):
for i, j in zip(range(len(dict)), range(len(dict1))):
for m, n in zip(range(len(dict[i])), range(len(dict1[j]))):
if dict[i][m] == dict1[j][n]:
print(dict[i][m])
for i in range(len(dict)):
for k in range(len(dict[i])):
#print(dict[i][k])
for j in range(dict[i+1][k], len(g)):
#print(j)
if dict[i][k] == dict[i+1][j]:
print(dict[i][k])
However, I ended up with index out of range error or unable to get the proper keys even before being able to extract the common repeating values. Does anybody know how to do this?