I have the following list list1 = [1, 2, 3, 3, 4, 3, 5, 5]
. I want to remove duplicates from the list only when the duplicate is after its original element. So in this case the output would be:
[1, 2, 3, 4, 3, 5]
I tried the following simple implementation:
for x in list1[:-1]:
NextIndex = list1.index(x)+1
if list1[NextIndex] != x:
O.append(x)
But of course it won't work because i'm doing if list1[NextIndex] != x
. I can't even use list1.remove()
, since removing from lists while looping doesn't work. Is there any way to do that with list comprehension maybe?