14/15 Your remove_duplicates modifies the list that was passed to it


#1

here is my code :smile:

> def remove_duplicates(x):
>     q = x
>     for i in range(len(q)-1):
>         for y in range(len(q)-2):
>             if q[i] == q[y]:
>                 q.remove(q[i])
>                 y = y+1
>     return q

it return error

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!

i don't know why ?? thank in advance :smiley:


#2

please close topic..my algorithm is not good


#3

Do you still need help? If so let me know and I will attempt to assist you.


#4

I have the same issue kindly tell me the mistake


#5

i figure out the issue
by typing q=x it stores the address of the list in x in q so basically q and x are same lists.
to copy x in q u need to make a list q=[] and append the values of x on q by for loop like:
q=[]
for item in x:
q.append(i)
instead of q=x
thn it will work


#6

thank you..i realize my algorithm is not right.so i don't need your help anymore :smiley:


#7

You could do it easier with the help of sets:
def remove_duplicates(el):
el=list(el)
el=set(el)
nel=list(el)
return nel

The sets module provides classes for constructing and manipulating unordered collections of unique elements.
https://docs.python.org/2/library/sets.html for more info.