0

I am getting the error "list assignment index out of range" in line 9 I have to get the key with the maximum gc content and print the gc content value along with the key. This is just an example I am doing, while the problem may contain more than 2 sequences.

d={"fff":'TTAGCCGAATTTGGC',"ddd":'TGATACTAGCGTAG'}
b=[]
a=[]
i=0 
for key in d:
    h=len(d[key])
    t=d[key].count('G')+d[key].count('C')
    content = (t*100)/h
    a[i]=content
    b[i]=key
    i+=1
val = max(a)
k = a.index(val)
print ("%s") % b[k]
print ("%f") % a[k]

1 Answers1

2

In contrast to some other scripting languages, you cannot enlarge Python lists by assigning to their out-of-range indices. To enlarge the list, you need to use the append method, i.e. replace:

a[i]=content
b[i]=key
i+=1

with:

a.append(content)
b.append(key)

The latter will do what you need, and is clearer to boot.

user4815162342
  • 141,790
  • 18
  • 296
  • 355