0

I have been running the following code o Jupyter Notebook 5.7.4 with Python 3.7.1. It worked fine there. When I tried to run the same code on HPC with python 3.5.2 I keep getting following error. if array[i-1] == array[i]: IndexError: list index out of range

import vcf
v = vcf.Reader(filename='/scratch/global/kkdesi01/Equine/animals/Chr/Chr11_possibleIntrogressionTargets.vcf')
f = open('/scratch/global/kkdesi01/Equine/animals/Chr/position11.txt', 'w+')
for record in v:
    f.write(str(record.POS))
    f.write('\n')
f.close()

with open('/scratch/global/kkdesi01/Equine/animals/Chr/position11.txt', 'r') as ins:
    array = []
    for line in ins:
        array.append(line)
print(len(array))

f = open('/scratch/global/kkdesi01/Equine/animals/Chr/filter11.txt', 'w+')
for i in range (1, len(array)):
    val1 = int(array [i-1])
    val2 = int(array [i])
    diff = val2-val1
    if diff < 10:
        f.write (str(val1))
        f.write ('\n')
        f.write (str(val2))
        f.write ('\n')
f.close()

with open('/scratch/global/kkdesi01/Equine/animals/Chr/filter11.txt', 'r') as ins:
    array = []
    for line in ins:
        array.append(line)
len(array)

for i in range(1, len(array)):
    if array[i-1] == array[i]:
        del array[i]

error if array[i-1] == array[i]: IndexError: list index out of range

I need help in understanding what needs to be changed in the code

Kash
  • 1
  • 1
  • You're modifying the array (a list, actually) while iterating. Usually not a good idea. – TrebledJ Jun 04 '19 at 18:18
  • 1
    `del array[i]` changes the length of the array. – Blorgbeard Jun 04 '19 at 18:18
  • 1
    Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – TrebledJ Jun 04 '19 at 18:19
  • 1
    Keep in mind that `range(1, len(array))` executes once before the loop starts. It doesn't shrink the range to account for the shrinking list being iterated over. – Carcigenicate Jun 04 '19 at 18:20
  • Thank you for your comments. Is there a better way to delete duplicates. I am new for coding, your help is much appreciated. – Kash Jun 04 '19 at 18:30
  • @Kalpi Note the duplicate that was suggested. It goes over many different ways. – Carcigenicate Jun 04 '19 at 18:36

1 Answers1

0

See below what is the problem.

You modify the length of the array during the iteration.

User set in order to remove duplicates.

array = [2, 3, 4, 4, 4]
for i in range(1, len(array)):
    if array[i - 1] == array[i]:
        print('Remove entry at offset {}'.format(i))
        del array[i]

output

Remove entry at offset 3

    if array[i - 1] == array[i]:
IndexError: list index out of range
balderman
  • 22,927
  • 7
  • 34
  • 52