Why does this keep on giving me a list index out of range error?
<pre><code>count = 0
badIndices = [59,64,68,72,74,77,79,103,104,108,109,118,119,123,124,130,133,139]
test1 = []
csvCourses = []
csvExamTime = []
examTime = []

with open("final_exam_schedule.csv") as file:
    reader = csv.reader(file, delimiter = ",")
    next(reader)
    for row in reader:
        csvCourses.append(row)

for i in range(len(csvCourses)):
    csvExamTime.append(csvCourses[i][5])
    examTime.append(findSchedule(csvCourses[i][4],csvCourses[i][1],csvCourses[i][3],csvCourses[i][2]))

for i in badIndices:
    test1.append(csvCourses[i][5])

for i in badIndices:
    if count &gt;= len(test1):
        break
    else:
        count += 1
        examTime[i] = test1[count] #this is where I get my error
</code></pre>

<p>every time I try to assign examTime[i] to test1, it gives me "out of range", but I thought that my if statement made sure that it didn't exceed the range?</p>

<p>For reference, examTime contains 155 different indices, and the badIndices list are the ones that aren't returning a value, so I'm going to that exact index and assigning it a value. findSchedule() is my function that has a bunch of ifs and elses to assign examTime, I didn't include it since it is really long and unnecessary to the problem. Only the specific bad indices don't return a value, the rest do.</p>

<p>edit: ok here is the findSchedule function</p>

<pre><code>def findSchedule(days,time,cr,period):          
    if days == "TR":
        if time == "11:00":
            if period == "AM":
                return("Tuesday, December 13, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")     
        elif time == "12:30" or time == "1:00":
            if period == "PM":
                return("Tuesday, December 13, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "3:30":
            if period == "PM":
                return("Tuesday, December 13, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "5:00":
            if period == "PM":
                return("Tuesday, December 13, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "6:30":
            if period == "PM":
                return("Tuesday, December 13, 7:00 - 9:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "8:00":
            if period == "AM":
                return("Wednesday, December 14, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "9:30":
            if period == "AM":
                return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
        elif time == "5:00" and cr == "5":
            return("Wednesday, December 14, 4:30 - 6:30 pm")
    elif days == "MWF":
        if time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "3:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "5:00":
            if period == "PM":
                return("Wednesday, December 14, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "6:30" or time == "8:00":
            if period == "PM":
                return("Wednesday, December 14, 7:00 - 9:00 pm")
            elif period == "AM" and time == "8:00":
                return("Thursday, December 15, 7:30 - 9:30 am")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "4:00":
            if period == "PM":
                return("Friday, December 16, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "8:00":
            if period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "3:30":
            return("Tuesday, December 13, 2:15 - 4:15 pm")
        elif time == "3:00":
            return("Wednesday, December 14, 2:15 - 4:15 pm")
        elif time == "11:00" and cr == "5":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
    elif days == "MW":
        if time == "8:00":
            if period == "PM":
                return("Wednesday, December 14, 7:00 - 9:00 pm")
            elif period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
        elif time == "9:00":
            return("Friday, December 16, 7:30 - 9:30 am")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "3:30":
            return("Tuesday, December 13, 2:15 - 4:15 pm")
        elif time == "6:30" and cr == "1":
            return("Monday, December 12, 6:30 - 7:25 pm")
        elif time == "6:30" and cr == "2":
            return("Monday, December 12, 6:30 - 8:20 pm")
        elif time == "6:30" and cr == "3":
            return("Monday, December 12, 6:30 - 9:30 pm")
        elif time == "6:30" and cr == "4":
            return("Monday, December 12, 6:30 - 10:15 pm")
        elif time == "5:00" and cr == "5":
            if period == "PM":
                return("Wednesday, December 14, 4:30 - 6:30 pm")

    elif days == "R":
        if time == "6:30" and period == "PM":
            return("Thursday, December 15, 7:00 - 9:00 pm")
    elif days == "WF":
        if time == "8:00" and (cr == "2" or cr == "5"):
            if period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
            elif period == "PM":
                return("No class found")
        elif time == "8:00" and cr == "4":
            if period == "AM":
                return("Wednesday, December 14, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class not found")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
            if period == "PM":
                return("Class not found")
        elif time == "9:30":
            if period == "AM":
                return("Friday, December 16, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class not found")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class not found")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class not found")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "12:30":
            if period == "PM":
                return("Tuesday, December 13, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Classs not found")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "3:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "3:30":
            if period == "PM":
                return("Tuesday, December 13, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "4:00":
            if period == "PM":
                return("Friday, December 16, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "5:00" and cr == "2":
            if period == "PM":
                return("Tuesday, December 13, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "5:00" and cr == "4":
            if period == "PM":
                return("Wednesday, December 14, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class not found")
        elif time == "9:30":
            return("Friday, December 16, 9:45 - 11:45 am")
        elif time == "3:30":
            return("Tuesday, December 13, 2:15 - 4:15 pm")
    elif days == "M":
        if time == "6:30":
            return("Monday, December 12, 6:30 - 7:25 pm")
    elif days == "T":
        if time == "6:30":
            return("Tuesday, December 13, 7:00 - 9:00 pm")
    else:
        if (cr == "4" or cr == "2") and time == "12:30":
            if period == "PM":
                return("Tuesday, December 13, 1:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "3:30":
            if period == "PM":
                return("Tuesday, December 13, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "5:00":
            if period == "PM":
                return("Tuesday, December 13, 4:30 - 6:30 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "8:00":
            if period == "AM":
                return("Wednesday, December 14, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "10:00":
            if period == "AM":
                return("Wednesday, December 14, 9:45 - 10:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "12:00":
            if period == "PM":
                return("Wednesday, December 14, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "3:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "5:00":
            if period == "PM":
                return("Wednesday, December 14, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "5" and time == "8:00":
            if period == "AM":
                return("Thursday, December 15, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "11:00":
            if period == "AM":
                return("Thursday, December 15, 9:45 - 11:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif cr == "4" and time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 12:00 - 2:00 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif cr == "5" and time == "2:00":
            if period == "PM":
                return("Thursday, December 15, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")
        elif time == "9:00":
            if period == "AM":
                return("Friday, December 16, 7:30 - 9:30 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "9:30":
            if period == "AM":
                return("Friday, December 16, 9:45 - 10:45 am")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "1:00":
            if period == "PM":
                return("Friday, December 16, 12:00 - 2:00 pm")
            elif period == "PM":
                return("Class final not found, please try again")
        elif time == "4:00":
            if period == "PM":
                return("Friday, December 16, 2:15 - 4:15 pm")
            elif period == "AM":
                return("Class final not found, please try again")   
        else:
            return("Class final not found, please try again")
</code></pre>

<p>It's tough to say without knowing what your data looks like, but here's what I think,</p>

<p>Your <code>count</code> variable is being incremented too early. In python list indexes start at <code>0</code> and go to <code>len(list)-1</code> as the last index. You're kind of starting at <code>count=1</code> so it's possible that on the last iteration <code>count=len(list)</code> which gives the error. Try this as a correction for the latter part of your code:</p>

<pre><code>for i in badIndices:
    if count &gt;= len(test1):
        break
    else:
        examTime[i] = test1[count]
        #We increment count after it is used
        #to avoid an error
        count+=1
</code></pre>

python