# Remove_duplicates question

#1

Its working

why does the if statement assume that the list new has inputs if i didn't append anything to it

``````def remove_duplicates(x):
new = []
for i in x:
if i not in new:
new.append(i)
return new``````

#2

It doesn't assume that the `list` has anything at all in it. In a series of iterations, it checks whether each `i` is not yet in the `list`. If a given `i` is not yet there, then it `append`s that `i` to the `list`.

The `list` starts out empty. Provided that `x` is not empty, the first item in `x` is not yet in the `new` list, and it gets `append`ed to it. In turn, each `i` in `x` gets checked, and `append`ed, if necessary.

#3

def remove_duplicates(lst):
new_lst = lst

``````j = 0
while j < len(lst):
k = j+1
while k < len(lst):
if lst[j] == lst[k]:
del(new_lst[k])
k = k + 1
j = j+1

return new_lst``````

print remove_duplicates([1,1,2,2])

Though I've made a copy of original list (lst) in a new list (new_lst) and modifying the new one, the original list is getting modified as well. Why is it so?

#4

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