14/15 List out of range, but the program runs without an error

This is my code, with some added prints for troubleshooting. It behaves exactly as I expect it to, yet I get an error when submitting saying that: “Oops, try again. remove_duplicates() resulted in an error: list index out of range”
I have tried running it on my debian server to doublecheck and I get no errors. It behaves exactly as expected and returns a list of numbers without any duplicates.

def remove_duplicates(x):
    l = len(x)
    print "length of x is: ", l
    newlist = [x[0]]
    counter = 0
    while counter < l -1:
        counter += 1
        print "counter:", counter
        if x[counter] not in newlist:
            print x[counter], "does not exist in list and is added to list "
            print x[counter], "exists in list"
    print newlist
    return newlist
print remove_duplicates([6,8,122,2,18,6,8,122,2,18])
1 Like


I used a simplified code per below:

def remove_duplicates(lst):
new_lst = []
for c in lst:
    if c not in new_lst:
return new_lst


Yeah, that worked beautifully.
Seems like I have a long way to go and one of my big problems is that I am overthinking the code.

no i getting the errro
while i using the same has you as writen above

sorry iguys i got my mistake

This One Worked Fine!!

def remove_duplicates(mylist):
newlist =
for item in mylist:
if not(item in newlist):
return newlist

1 Like

Check out this:

def remove_duplicates(x):
    for i in x:
        if i not in y:
    return y

wow i really have to stop over complicating it

This worked for me:

import copy

def remove_duplicates(lista):
l =
l = copy.deepcopy(lista)

new_list = list(set(l))
return new_list

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

1 Like

This code works good and it’s simpler.
def remove_duplicates(numbers):
for i in numbers:
if i not in newlist:
return newlist

wow nice code man :smiley:

Thank you all. If anything you helped me understand that there is a lot of flexibility in this and we only have to prompt for the correct action.
I love this game.

I had the same problem. Your code looks fine to me as well. Even if there are simpler ways, I don’t understand the ‘index out of range’ issue.

I made a similar post. I Hope one day someone can solve this “index out of range” mystery

def remove_duplicates(l):
return list(set(l))

I used this shockingly simple code as well, couldnt believe it worked
def remove_duplicates(bubs):
new_list =
for n in bubs:
if n not in new_list:
return new_list

guess i didn’t even need the else continue it looks like

thanks mshoare…it’s good code you have here…:grin:

That is almost EXACTLY what I did!!! Just saying’. I think that’s cool!

Thanks mate!

Regards Matt.

I use a similar code as well. This was probably covered earlier but I can’t remember how to print out the result. I want to see the new_list. print new_list does not work. This has been bothering me for the last 5 or so lessons in the course. I have been ignoring that I am unable to see the results. But now its driving me crazy and I have tried several thing.


lst = raw_input("list of numbers please")
print lst
def remove_duplicates(lst):
    new_list = []
    for d in lst:
        if d not in new_list:
    return new_list  
print new_list