Remove_duplicates HELP!


#1

def count(lst,item):
    count = 0
    for item in lst:
        count += 1 

def remove_duplicates(lst):
    new = lst
    for i in lst:
        if i in lst and count(lst,i) >= 2:
            del i
    return new

I created a new def and used it inside my remove_duplicate , but code cademy says this:
"Oops, try again. remove_duplicates([4, 5, 5, 4]) returned [4, 5, 5, 4] instead of [4, 5]"


#2

Be advised these are one in the same object. new is not a copy, but a reference to lst To copy a list we take a full slice.

new = lst[:]

When using a look-up loop,

for item in lst:

item cannot be changed inside the loop. To modify a list we must iterate over a range and refer to the index. So in other words, back to the drawing board.

Consider creating a new list and appending each unique item to that list, then return.

uniques = []

if item not in uniques:

#3

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