Remove_duplicates


#1

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


#2

Be sure the return statement is not in the loop block.


Aside

l we would assume is short for, list. The question this raises is, "What kind of list?"

And, "Is that a 1 or an l? (one or an elle)

The way to avoid this kind of confusion is to not write code using l as a variable. Your list is a local variable that collects unique values. A local name like, 'uniques' would exactly define the object.

uniques = []

#3

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

Oops, try again. remove_duplicates([4, 5, 5, 4]) resulted in an error: argument of type 'NoneType' is not iterable


#4

Having addressed one error we can move to the next which is,

l.append(a)

is a statement. We cannot assign a stratement to a variable. As it stands, the action implied by your code still takes effect.


#5

Hi,mtf,
it's ok, than you !


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

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.