Remove_duplicates


#1

I'm having difficulty getting my code to work for this exercise. Why doesn't this work?

def remove_duplicates(lst):
    new_list = [lst[0]]
    n = 1
    for i in lst:
        if i != lst[n-1]:
            new_list.append(i)
        n += 1
    return new_list


#2

take a look:

def remove_duplicates(lst):
    new_list = [lst[0]]
    n = 1
    for i in lst:
        print i, lst[n-1]
        if i != lst[n-1]:
            new_list.append(i)
        n += 1
    return new_list
print remove_duplicates([4, 5, 5, 4])

you are comparing the number in lst with itself! That won't work. you need to check if the number isn't alreayd in new_list. by either another loop, or if not in


#3
def remove_duplicates(inputs):
    result=[]
    for values in inputs:
        result = values
    return result

Now this is weird!! i was debugging my code and when i tested the above code it went through!! what i dont understand is why it worked!! i can tell the code is wrong but codecademy approved it!! BUG? MAGIC?


#4

it shouldn't go through, but some corner cases go through, it is inevitably.