Stuck for a while with remove_duplicates... please help me find the error!


I have been trying to solve remove_duplicates without the hint and cant seem to find the logic error. Can anyone else point it out for me?
(I am used to using Java, so I know this is a lot more complicated than necessary, but I still want to understand my mistake)

def remove_duplicates(lst1):
    lst2=lst1 # list to work on and return
    for i in lst2: 
        while (x<len(lst1)) and (lst1[x]!=i): #iterates until finds duplicate
        if lst1[x]==i: #if finds duplicates, and not just end of list
            for p in lst2: #removes dups from whole list
    return lst2



lst2=lst1 # list to work on and return

This will not create a copy of the list. So, now you have two variable which point to the same list.

the for loop make sense, but then i lose it. How is the while loop finding duplicate values?


the while statement stops when either it gets to the end of the list, or it finds a duplicate (lst1[x]=i).
the next if statement check why the while stopped, and if x was found to be a duplicate, it removes all x from lst 2


not sure that works, but that is currently certainly not what happens

given the if statement is the same indention level as the while loop, the while loop simply runs till the end, the comes to the if statement


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