pythonrosalind

Why do I keep getting an index error when trying to delete, when replacing with a character works fine?


Programming newb/python newb, my job is super undemanding so I've found a lot of free time to teach myself how to code.

I'm working this rosalind.info problem.

Here is my code so far:

# -*- coding: utf-8 -*-
"""
Created on Thu Jan 21 09:01:51 2016

@author: aseyedian
"""
codon = ''

q=0
with open('rosalind_prot.txt', 'r') as prot:
    bb = list(prot.read())

mylist = []   
for i in range(len(bb)):
     mylist.append(bb[i])


for i in range(0, len(bb),3):
    mylist[i] = [''.join(mylist[i:i+3])]

for i in range(1, len(mylist), 3):
    del mylist[i]

for i in range(2, len(mylist), 3):
    del mylist[i]

print mylist
#This is to create a list of codons which then are translated into amino acids

for some reason,

for i in range(1, len(mylist), 3):
        del mylist[i]

returns an IndexError (list assignment out of range), however,

for i in range(1, len(mylist), 3):
        mylist[i] = 'k'

Turns every second list member into the letter 'k'. Why is this?

FYI, what I'm trying to do right now is put every nucleotide into a list, and then group them by three's, then delete the two following list members after incorporating them into the first one, then moving on to the next codon.

For example: ['A', 'U', 'G'] -> [['AUG'], 'U', 'G'] -> ['AUG'], ['GCC' (the next codon)], etc...


Solution

  • Don't copy bb to mylist and then try to modify mylist in place. Just copy from bb to mylist:

    bb = 'AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA'
    mylist = []
    for i in range(0, len(bb), 3):
        mylist.append(bb[i:i+3])
    print mylist
    

    Output:

    ['AUG', 'GCC', 'AUG', 'GCG', 'CCC', 'AGA', 'ACU', 'GAG', 'AUC', 'AAU', 'AGU', 'ACC', 'CGU', 'AUU', 'AAC', 'GGG', 'UGA']