14. Remove_duplicates code works but i receive an error


Oops, try again. 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!

I don; t get why i receive this problem - the code works, we take an input list, copy it, modify our COPY, then return the new version, so i don't understand why i receive that message ?

def remove_duplicates(lst):
    for i in range(0,len(lst2)):
        for q in range (i+1,len(lst2)):
            if lst2[i]==lst2[q]:
    return lst2


there is one small problem, this:


won't make a copy of the lst. now lst2 contains a reference to lst.

to make a copy, you would need to construct a loop and append items:

lst2 =[]
for i in lst:

or if you are lazy, use the built in list function:

lst2 = list(lst)


can you give us the code?


asking or giving the code is against the guidelines, if you experience problems please make your own topic


my code is this:

def remove_duplicates(y):

      for i in range(0,leng-1):
          for q in range (i+1,leng-1):
              if lista[i]==lista[q]:
      return lista

It works on my computer, but it doesn't on the site


You say it works on your computer but did you test it the same way? Not finding a bug doesn't mean there isn't one, and changing two things about how you test your code doesn't allow you to determine which one is responsible.


the exercise test many lists, do you also do this on your computer?

pop() is a really difficult method, since the loop you are looping over becomes shorter.

I really recommend a different approach


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