You could simplify your code to the following

def get_amino_acid(rna):
    xdict = {
      "Phenylalanine": ["UUU", "UUC"],
      "Leucine": ["UUA", "CUU", "CUC", "CUA", "CUG", "UUG"],
      "Isoleucine": ["AUU", "AUC", "AUA"],
      "Methionine": ["AUG"],
      "Valine": ["GUU", "GUC", "GUA", "GUG"],
      "Serine": ["UCU", "UCC", "UCA", "UCG"],
      "Proline": ["CCU", "CCC", "CCA", "CCG"],
      "Threonine": ["ACU", "ACC", "ACA", "ACG"],
      "Alanine": ["GCU", "GCC", "GCA", "GCG"],
      "Tyrosine": ["UAU", "UAC"],
      "Histidine": ["CAU", "CAC"],
      "Glutamine": ["CAA", "CAG"],
      "Asparagine": ["AAU", "AAC"],
      "Lysine": ["AAA", "AAG"],
      "Asparatic Acid": ["GAU", "GAC"],
      "Glutamic Acid": ["GAA", "GAG"],
      "Cysteine": ["UGU", "UGC"],
      "Trytophan": ["UGG"],
      "Arginine": ["CGU", "CGC", "CGA", "CGG", "AGG", "AGA"],
      "Glycine": ["GGU", "GGC", "GGA", "GGG"]
    }

    return [key for key, value in xdict.items() if rna in value]

Then it will handle cases where no amino acid contains that rna

>>> get_amino_acid('UCU')
[]
>>> get_amino_acid('AGU')
['Serine']
>>> get_amino_acid('CAC')
['Histidine']

Also as noted by @Carcigenicate you have an additional "Serine" key that can be removed.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top