-1

Is there a way to preserve the original order of elements from the list? with my code below I get this as the output

Values: [1, 3, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0]

Clean: [5, 2, 4, 1, 3, 0]

I need [1, 2, 0, 4, 5, 3] as the "clean" list

def remove_duplicates(lst)
 i = len(lst)
 while i>0:
  while values.count(values[i]>1:
   values.remove(values[i])
   i-=1
  i-=1
 return

The question seems pretty simple to solve with for loops and a new list output but I am required to use while loops and stick with only one list.

def remove_duplicates(lst):
 new =[]
 for x in lst:
  if x not in lst:
   new.append(x)
 return new
MSeifert
  • 145,886
  • 38
  • 333
  • 352
Miryloth
  • 55
  • 1
  • 2
  • 9
  • 3
    How you got the desired list as `[1, 2, 0, 4, 5, 3]`? I do not see any logic behind it. At least this list is neither based on the first occurrence of number nor the last occurrence. Please explain the logic – Moinuddin Quadri Jan 13 '17 at 00:20
  • @Miryloth your output makes no sense. If you were to take the first instance of each number in your list you would get `[1,3,0,4,2,5]` but you are saying it should be `[1, 2, 0, 4, 5, 3]`? – Paul Rooney Jan 13 '17 at 00:52
  • Possible duplicate of [How do you remove duplicates from a list in whilst preserving order?](http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-whilst-preserving-order) – Anthony Pham Jan 13 '17 at 01:04
  • My apologies you're right it should be [1,3,0,4,2,5] – Miryloth Jan 13 '17 at 01:12

1 Answers1

1
def remove_duplicates(lst):
    i = 0
    while i < len(lst):
        j = i + 1
        while j < len(lst):  # check for duplicates of lst[i] and remove them
            if lst[i] == lst[j]:
                del lst[j]
            else:
                j += 1  # only increment second idx if the item is not removed!
        i += 1
    return

And testing it:

>>> lst = [1, 3, 0, 1, 4, 1, 1, 2, 2, 5, 4, 3, 1, 3, 3, 4, 2, 4, 3, 1, 3, 0, 3, 0, 0]
>>> remove_duplicates(lst)
>>> lst
[1, 3, 0, 4, 2, 5]

You could also implement it with a set instead of the second while loop (which is definetly faster but I'm not sure if allowed!):

def remove_duplicates(lst):
    i = 0
    found = set()
    while i < len(lst):
        if lst[i] in found:
            del lst[i]
        else:
            found.add(lst[i])
            i += 1
    return

Just a quick note about why you're approach couldn't work can be found in the documentation of list.remove:

list.remove(x)

Remove the first item from the list whose value is x. It is an error if there is no such item.

But you want to remove all occurences except the first one!

Community
  • 1
  • 1
MSeifert
  • 145,886
  • 38
  • 333
  • 352