14. Remove_duplicates code works but i receive an error


#1


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):
    lst2=lst
    for i in range(0,len(lst2)):
        for q in range (i+1,len(lst2)):
            if lst2[i]==lst2[q]:
                lst2.pop(q)    
    return lst2


#2

there is one small problem, this:

lst2=lst

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:
   lst2.append(i)

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

lst2 = list(lst)

#4

can you give us the code?


#5

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


#6

my code is this:

def remove_duplicates(y):
      lista=list(y)
      leng=len(lista)

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

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


#7

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.


#8

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


#9

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