Python zero division error. float division by zero. simple script


I am trying to convert an array into a percent change array. it is simple, but I do not know why I am getting a zero division error. I tried putting

from __future__ import division

at the top of my file, but no dice.

my code:

def convert(anarr):
     x = 1
    while(x < len(anarr)):
        anarr[x] = (anarr[1] - anarr[x])/anarr[1]
        x += 1
    print anarr


>>> myarr = [20130101.0,34.75,34.66,34.6,34.6,34.61,34.65,34.69]
>>> convert(myarr)
>>> Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
  File "C:\Users\viral\Desktop\python\", line 38, in convert
    anarr[x] = (anarr[1] - anarr[x])/anarr[1]
ZeroDivisionError: float division by zero

Since you are modifying the array as you go, one of the elements is becoming 0 which causes the error. There are consecutive 34.6s in the array which have a percentage change of 0. Put the changes to a new array, and copy later if you want.