<p>You need to iterate over the sequence 3 characters at a time and you need to stop when you hit the stop codon (or before for an invalid codon, e.g. of length 2). Here is a memory efficient implementation using iterators (requires python &gt;= 3.8):</p>
<pre><code>from itertools import islice

def protfromRNA(Seq):
    it = iter(Seq)
    lst = []
    while codon := &quot;&quot;.join(islice(it, 3)):
        aa = GeneticCode.get(codon)
        if aa is None:
            raise ValueError(f&quot;invalid codon: {codon}&quot;)
        if aa == &quot;X&quot;:
            break
        lst.append(aa)
    return &quot;&quot;.join(lst)

print(protfromRNA('GCCAACGCACCCACUUUUUUUUAGAAA'))
# ANAPTFF
</code></pre>

CC BY-SA 4.0
2021-11-09T22:23:24.283Z
2021-11-09T22:23:24.283Z
Chris_Rands
<p>I'm trying to create a python program that follows the codon translation and turn mRNA sequence into amino acids (see the dictionary). The system has to return an error of the string is not a multiplier of 3, and the the function should not produce any more amino acids once a stop codon (keys with a value of X in the dictionary) is found, and should not display 'X' in the output. I do not know how to incorporate the stop codon to stop the programming entirely, as well as the multiplier of 3 aspect in the code. Can someone give me any advice? Thank you so much!
example: protfromRNA('GCCAACGCACCCACUUUUUUUUAGAAA') should return ANAPTFF</p>
<pre><code>def protfromRNA(Seq):
    GeneticCode = {'AAA':'K', 'AAC':'N', 'AAG':'K', 'AAU':'N',
               'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACU':'T',
               'AGA':'R', 'AGC':'S', 'AGG':'R', 'AGU':'S',
               'AUA':'I', 'AUC':'I', 'AUG':'M', 'AUU':'I',
               'CAA':'Q', 'CAC':'H', 'CAG':'Q', 'CAU':'H',
               'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCU':'P',
               'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGU':'R',
               'CUA':'L', 'CUC':'L', 'CUG':'L', 'CUU':'L',
               'GAA':'E', 'GAC':'D', 'GAG':'E', 'GAU':'D',
               'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCU':'A',
               'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGU':'G',
               'GUA':'V', 'GUC':'V', 'GUG':'V', 'GUU':'V',
               'UAA':'X', 'UAC':'Y', 'UAG':'X', 'UAU':'Y',
               'UCA':'S', 'UCC':'S', 'UCG':'S', 'UCU':'S',
               'UGA':'X', 'UGC':'C', 'UGG':'W', 'UGU':'C',
               'UUA':'L', 'UUC':'F', 'UUG':'L', 'UUU':'F'}
    return ''.join(GeneticCode[i] if GeneticCode[i]!=X else '' for i in Seq)
</code></pre>

<python><codon>
list comprehension and codon translation
# list comprehension and codon translation

(Asked by: username_0 on 2021-11-09)

I'm trying to create a python program that follows the codon translation and turn mRNA sequence into amino acids (see the dictionary). The system has to return an error of the string is not a multiplier of 3, and the the function should not produce any more amino acids once a stop codon (keys with a value of X in the dictionary) is found, and should not display 'X' in the output. I do not know how to incorporate the stop codon to stop the programming entirely, as well as the multiplier of 3 aspect in the code. Can someone give me any advice? Thank you so much!
example: protfromRNA('GCCAACGCACCCACUUUUUUUUAGAAA') should return ANAPTFF
def protfromRNA(Seq):
    GeneticCode = {'AAA':'K', 'AAC':'N', 'AAG':'K', 'AAU':'N',
               'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACU':'T',
               'AGA':'R', 'AGC':'S', 'AGG':'R', 'AGU':'S',
               'AUA':'I', 'AUC':'I', 'AUG':'M', 'AUU':'I',
               'CAA':'Q', 'CAC':'H', 'CAG':'Q', 'CAU':'H',
               'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCU':'P',
               'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGU':'R',
               'CUA':'L', 'CUC':'L', 'CUG':'L', 'CUU':'L',
               'GAA':'E', 'GAC':'D', 'GAG':'E', 'GAU':'D',
               'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCU':'A',
               'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGU':'G',
               'GUA':'V', 'GUC':'V', 'GUG':'V', 'GUU':'V',
               'UAA':'X', 'UAC':'Y', 'UAG':'X', 'UAU':'Y',
               'UCA':'S', 'UCC':'S', 'UCG':'S', 'UCU':'S',
               'UGA':'X', 'UGC':'C', 'UGG':'W', 'UGU':'C',
               'UUA':'L', 'UUC':'F', 'UUG':'L', 'UUU':'F'}
    return ''.join(GeneticCode[i] if GeneticCode[i]!=X else '' for i in Seq)

**Tags:** python, codon

---

## 1 answers

### Answer by username_1 on 2021-11-09. Score: 1

You need to iterate over the sequence 3 characters at a time and you need to stop when you hit the stop codon (or before for an invalid codon, e.g. of length 2). Here is a memory efficient implementation using iterators (requires python >= 3.8):
from itertools import islice

def protfromRNA(Seq):
    it = iter(Seq)
    lst = []
    while codon := "".join(islice(it, 3)):
        aa = GeneticCode.get(codon)
        if aa is None:
            raise ValueError(f"invalid codon: {codon}")
        if aa == "X":
            break
        lst.append(aa)
    return "".join(lst)

print(protfromRNA('GCCAACGCACCCACUUUUUUUUAGAAA'))
# ANAPTFF


---

