Remove Duplicates


#1

I’m a little confused b/c I ran my code on my desktop console and it works fine. then when i run it on codeacademy it says this:

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

But have I not removed the duplicates? My code is copied below

def remove_duplicates(x):
y = x
y.sort()
leng = len(y) - 2
for i in range(0,leng):
if y[i] == y[i+1]:
del y[i+1]
print y


#2

yes, except the exercise doesn’t allow you to modify the original list, and this:

y = x

won’t make a copy of the list, it will just create a variable pointing to the same list in memory


#3

I totally forgot about that! Thanks for pointing that out :slight_smile:
would i have to use a function like copy.copy() ?
if i placed x into another list i would have to make a nested loop to access y[0][i] …if that makes any sense.

can you give me any hints on how to debug?
thanks in advance!


#4

if you function works correctly i would simply make an identical copy, there are several ways this can be done (creating an empty list, loop over the original list and append to the empty list, built in function like list() or list slicing can also give us a copy of a list)


#5

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