# Remove_duplicates

#1

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!

In the second line I made a copy of the input and only modify the copy, so I don't understand why I'm getting this error.

``````def remove_duplicates(lst):
copy = lst
count_a = len(copy) - 1
while count_a >= 0:
count_b = count_a - 1
while count_b >= 0:
if copy[count_b] == copy[count_a]:
copy.remove(copy[count_b])
count_a -= 1
count_b -= 1
else:
count_b -= 1
count_a -= 1
return copy``````

#2

By doing this, you're actually not making a copy but rather making a new name for `lst`. So whatever you do to `copy` you also do to `lst` since they both point to the same list. If you want to create a new list, just do `copy = list(lst)`.

#3

Oh wow i didnt realize that. It works now, thanks!

#4

Another possibility is slicing it.

`copy = lst[:]`

But, `copy = list(lst)` is more readable.

#5

``````            if copy[count_b] == copy[count_a]:
copy.remove(copy[count_b])
count_a -= 1
count_b -= 1``````

why unnecessarily decrease count_a here? we still have to compare the number placed at position count_a with the other left elements.

#6

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