14. remove_duplicates


#1
def remove_duplicates(lists):
    new = lists[0]
    for n in range(1, len(lists)):
        if lists[n] not in new:
            new.append(lists[n])
    return new

I know the right code, just cannot find anything wrong with my old codes above:

It returns the error below, wondering can anyone help me figure out what's wrong with my code above? Thanks!

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


#2

It is quite simple why it is not functioning.

Do the following

def remove_duplicates(lists):
    new = lists[0]
    print(type(new)) # This will tell you why you keep getting an error
    for n in range(1, len(lists)):
        if lists[n] not in new:
            new.append(lists[n])
    return new

If you still do not have enough information feel free to ask.


#3

Look at your second line.
new = lists[0]
that sets new to the first element in the list. That element is an int, so it cannot be iterated.

just
new = []

Should resolve :slightly_smiling:


#4

Got it! Thanks!:slightly_smiling:


#5
def remove_duplicates(lists):
    new = []
    for n in range(len(lists)):
        if lists[n] not in new:
            new.append(lists[n])
    return new

Thanks! I corrected it like this and it works!