0

I have a dictionary where for a single key there is more than one value. From the dictionary i want to check the value to find the key and also find the index of the value.

genDict2= {"A":["GCT","GCC","GCA","GCG"],
           "B":["TAA","TGA","TAG"],
           "C":["TGT","TGC"],
           "D":["GAT","GAC"]}

alphaSet =[]
for i in range(len(genCollect)):
    for k, v in genDict2.items():
        if genCollect[i] in v:
            alphaSet.append(k)
print(alphaSet)

From this code I can find the key but don't know how to find the index of the value. Suppose if the input is 'GCC TAG GAT' then the output should be 'ABD' and ' 120'.

Nikaido
  • 4,443
  • 5
  • 30
  • 47
Sajib
  • 19
  • 3
  • This might a good opportunity to work with `pandas`. – Yaakov Bressler Sep 05 '19 at 16:33
  • 1
    If you are going to need to do lots of lookups in a fairly small data set (yours will be 64 max), it's often faster/easier to transform the look up table to directly give you what you need. i.e. convert `{"A": ["GCT", "GCC"], "B": ["TAA"]}` to `{"GCT": ("A", 0), "GCC": ("A", 1), "TAA": ("B", 0)}` – Nick T Sep 05 '19 at 16:36

1 Answers1

0

You can use the list method index

genDict2= {"A":["GCT","GCC","GCA","GCG"],
           "B":["TAA","TGA","TAG"],
           "C":["TGT","TGC"],
           "D":["GAT","GAC"]}

alphaSet =[]
genCollect = ["GCT", "GCC", "GCA"]
for i in range(len(genCollect)):
    for k, v in genDict2.items():
        if genCollect[i] in v:
            alphaSet.append((k, v.index(genCollect[i])))
print(alphaSet)
Nikaido
  • 4,443
  • 5
  • 30
  • 47