0

I'm relatively new to python and I've been working on an algorithm that involves genetic code. I started by associating all 4 genetic bases, A, C, T, G with a list. A is 1,0,0,0. C is 0,1,0,0. T is 0,0,1,0 and G is 0,0,0,1. There are two different genetic codes, one being the original one and the other being one that was genetically mutated. The algorithm is going to come to conclusions of the data given based on the difference between the two genetic codes. But first, I need to sort of preprocess the data before I can work on the algorithm making conclusions.

The code looks like this so far:

A = [1, 0, 0, 0]
C = [0, 1, 0, 0]
T = [0, 0, 1, 0]
G = [0, 0, 0, 1]
original = [A, T, T, G, C, T, A]
copy = [C, T, T, A, T, A, A]
final = original
for i, v in enumerate(original):
    if v == copy[i]:
        print(v)
    elif v != copy[i]:
        final.insert(i, 1)
        print(final)

The error that I'm getting is that "index is out of range". However, the program still runs but the error shows up. Firstly, should I be worried about this in the first place and does it mean anything? Second, if it is a big deal, how should I fix this? If it's not, why is it happening in the first place? I also tried different code compliers like google collab and Pycharm. enter image description here

I'm fairly new to Python so there could be something small I'm overlooking. Any help would be really appreciated!

Austin
  • 25,759
  • 4
  • 25
  • 48
Rishi
  • 41
  • 4
  • 1
    The program only runs until it encounters the error. then it throws the error and does not run anymore. – warped Jun 18 '20 at 13:36

2 Answers2

3

This happens because final and original point to same location in memory. So when you inserted values to final at this line of code:

final.insert(i, 1)

... original variable got also reflected and in the end there was no value at the ith location in copy here:

if v == copy[i]:

Solution:

You have create a copy of list:

final = original[:]
# or: final = original.copy()
Austin
  • 25,759
  • 4
  • 25
  • 48
1

The root cause of the problem is this line:

final = original

You may be thinking that final is a new list which contains the same values as original. But in fact, it refers to the same list.

Now, in this loop,

for i, v in enumerate(original):
        # ...
        final.insert(i, 1)

you not only modify the final list, but, since it's actually the same list, also original. Therefore, the iteration over original is extended and leads to values of i which are larger than what is allowed as index for the copy list (which originally had the same size as original).

Therefore you get the error "List index out of range" for the line if v == copy[i], which effectively means that i has become larger than len(copy) - 1.

To avoid this error, you need to create final as an actual copy of original, which you can do by

final = original[:]

or

final = list(original)

See How to clone or copy a list? for even more options.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65