I suppose to split this sequence into a list of n=3.
codons('agucaccgucautc')
# result = ['agu','cac','cgu','cau']
# 'tc' is supposed to be ignored as it doesn't equal to n=3
I've tried the following solution;
def codons(RNA):
"""This functions returns a list of codons present in an RNA sequence"""
# store the length of string
length = len(RNA)
#divide the string in n equal parts
n = 3
temp = 0
chars = int(len(RNA)/3)
#stores the array of string
change = []
#check whether a string can be divided into n equal parts
for i in range(0, length, chars):
part = [RNA[i:i+3] for i in range(0, length, n)];
change.append(part);
return part
if (length % n != 0):
continue
But when I try to run the previous code again, it still returns 'tc'
codons('agucaccgucautc')
# result = ['agu', 'cac', 'cgu', 'cau', 'tc']
Can anybody help me what should I do to ignore any chars that not equal to n=3 or the last part 'tc'?