14/15 List index out of range


#1

I know there are other ways to solve this, but can anyway explain why I get "list index out of range" for this? It works and the index's seem right to me.

def remove_duplicates(my_list):

new_list=[my_list[0]]
for element in range(0,len(my_list)):
    for already in range(0,len(new_list)):
        print "len(new_list)=: " + str(len(new_list))
        #if equal, then break - don't append
        if new_list[already] == my_list[element]:
            break
    #else is not equal...append to new_list
    else:
        print "new_list:" + str(new_list)
        new_list.append(my_list[element])
for i in range(0,len(new_list)):
    print str(i)
print "new_list:" + str(new_list)
print "my_list:" + str (my_list)
return new_list

print remove_duplicates([2,1,2,3,2])
print remove_duplicates([1,1,3,4])


#2

That code doesn't run, it has a return outside of a function.
And new_list is missing.
And after fixing those things, it doesn't trigger any index out of range error.

Provide code that produces what you're asking about, otherwise we don't have much to go by!


#3

It runs fine for me. new_list is the second line of the code. Don't forget the "def remove_duplicates(my_list):" line. If I take out the carriage return above, codeacademy removes all the indentation for some reason.