With a nod to Óscar, who figured out the bulk of the problem, I think the OP is asking about something like this:
codon = 'AACTGCAGCTCA'
list = [codon[i:i+3] for i in range(0, len(codon), 3)]
=> ['AAC', 'TGC', 'AGC', 'TCA']
The list ['AAC', 'ACT', 'CTG', 'TGC', 'GCA', 'CAG', 'AGC', 'GCT', 'CTC', 'TCA']
was an unintended result of the OP's code, because each triplet contains the last two characters of the previous one.
Edit: Also, this chunk of code:
for p in patCodon:
for d in dna:
if d == p:
x = dna[p]
print (p)
patCode.append(x)
should probably be this instead:
for p in patCodon:
if p in dna:
x = dna[p]
print (p)
patCode.append(p)
The reason is that checking for membership with in
is much faster than looping over the members.
This will only work if dna
is a dict. If dna
is a list, the same syntax will work to check whether p
is in dna
, but x = dna[p]
is probably a mistake.