14/15.remove_duplicates error


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/4

Oops, try again.
Your remove_duplicates modifies the list that was passed to it. [4,
5, 5, 4] was modified to [5, 4]. Perhaps you should make a copy first!


So, as it's said in the brief, the new list with duplicate numbers was created, and then the old one was cleared from duplicate, but i still have error message like i have just modified the first list without creation of another one. Point me if i'm wrong somewhere.


def remove_duplicates(x):
    t = []
    for n in x:
        if n not in t:
            t_n = t.append(n)
    for n in x:
        if n in x:
            x.remove(n)
            
    return t_n


#2

Our list of uniques is t so we don't need yet another list.

    t.append(n)

is all we need. At the completion of the for loop iteration, simply return t. Nothing else to do. The second for statement is unnecessary.


#3

Hm, yes, you was right. I though that i also need to remove that duplicate values from original list after copying them to new one.


#4

By returning the shortened list, you are completing your objective. The original list remains intact, which is generally the wise thing to do. When we pass a list to a function, it doesn't get copied. Any changes we do to the list inside the function are applied to the list in calling scope.

Our function is manufacturing a secondary list without mutating the first. If our objective is to mutate the main list, then we could do it by assigning the return value.

    a = [7,8,7,6,5,4,3,2,3,4,3,5,6,5,5,4]
    a = remove_duplicates(a)
    a.sort()
    print a    # [2, 3, 4, 5, 6, 7, 8]