Question in "remove_duplicates"


#1



In "remove_duplicates"exercise, my code is as followed. there is not syntax error but the result is wrong. what's wrong with my code?


def remove_duplicates(numbers):
    new_numbers=numbers
    for i in range(len(numbers)):
        for j in range(i+1,len(numbers)):
            if numbers[i]==numbers[j]:
                new_numbers.pop(i)
                break
    return new_numbers


#2

You need to account for that removing elements causes values to move in the list.
Also note that assignment does not create a copy, so new_numbers and numbers are referring to the same list.


#3

thank you for reminding the use of .pop() which i really didn't notice.
however, i think that this is not the main problem of my code since my code couldn't function even after fixing the problem you had mentioned


#4

If it's still not working then how can you tell you fixed it? I'm very confused, and my guess is that you just didn't fix it.

Observe what your code does and compare to what you want it to do. Find the difference.


#6

Hi all,

I solved this practice as the following code;


#7

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