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


#1

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:
            newlist.append(x[counter])
            print x[counter], "does not exist in list and is added to list "
        else:
            print x[counter], "exists in list"
    print newlist
    return newlist
print remove_duplicates([6,8,122,2,18,6,8,122,2,18])

#2

Hi,

I used a simplified code per below:

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

#3

Hi!

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.


#4

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


#5

sorry iguys i got my mistake


#6

This One Worked Fine!!

def remove_duplicates(mylist):
newlist = []
for item in mylist:
if not(item in newlist):
newlist.append(item)
return newlist


#7

Check out this:

def remove_duplicates(x):
    y=[]
    for i in x:
        if i not in y:
            y.append(i)
    return y

#8

wow i really have to stop over complicating it


#9

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])


#10

This code works good and it's simpler.
def remove_duplicates(numbers):
newlist=[]
for i in numbers:
if i not in newlist:
newlist.append(i)
return newlist


#11

wow nice code man :smiley:


#12

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.


#13

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.


#14

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


#15

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


#16

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:
new_list.append(n)
else:
continue
return new_list

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


#17

thanks mshoare...it's good code you have here..:grin:


#18

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


#19

Thanks mate!

Regards Matt.


#20

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.

Thanks

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:
            new_list.append(d)
    return new_list  
print new_list