Python IndexError: list index out of range

Replace:

for i in range(1, 31):

with:

for d in Data[1:31]: #since you have range(1,31). Do Data[1:] if you just want to skip the first
    OneDate.append(d[0])
    OneClose.append(d[4])

This usually happends when Data array has less than 31 indices. Also ensure d array has atleast 5 items, else d[4] will also throw a similar error. Use:

if len(d) >= 5:  #check first.
    OneDate.append(d[0])
    OneClose.append(d[4])

Leave a Comment