Return a list of codons from a sequence

There are no built-in functions (that I know of) for returning a list of codons from a sequence, but making your own is quite simple. This function is from Solution A.5 from the Python Course in Bioinformatics


def codons(s, frame=0):
    """Return a list of codons from a string, s, giving an optional frameshift."""

    codons=[]

    end=len(s[frame:]) - (len(s[frame:]) % 3) - 1

    for i in range(frame,end,3):
        codons.append(s[i:i+3])

    return codons

1 Response to “Return a list of codons from a sequence”


  • Hey just wanted to let you know. I am not sure what build of python you are using, but for 2.6.1 if you want the code to work properly, you need parentheses around the last part of the end equation.

    end = len(s[frame:]) – ((len(s[frame:]) % 3) – 1)

Leave a Reply