Practice makes perfect: removing duplicates


#1

Hi whenever i run the code bellow i get the following error.

Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [] instead of [4, 5]

I really don't understand why this is! Any advice is greatly appreciated

Thanks

def remove_duplicates(l1):
    l2 = []
    for n in l1:
        if n not in l1:
            l2.append(n)
        return l2

#2

you should check if n is not already in l2, all the values are in l1, so nothings gets append to l2.


#3

sorry i don't understand what you mean by this should i put

l1.append(n)


#4

no, the problem is here:

if n not in l1:

all the numbers are in l1, so this condition will always be false. You have to check with the if condition if n is not yet in l2


#5

ok i see but when i do this i now get this error

Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [4] instead of [4, 5]

def remove_duplicates(l1):
    l2 = []
    for n in l1:
        if n not in l2:
            l2.append(n)
        return l2

#6

a function ends the moment a return keyword is reached, this currently is happening in the first iteration of the loop

change the indent of return so the whole for loop can run


#7

Thanks so much!!! that now works

def remove_duplicates(l1):
    l2 = []
    for n in l1:
        if n not in l2:
            l2.append(n)
    return l2

#8