I am trying to delete duplicate elements in the list. The code below says index out of range when the value of i
goes to 9
. 9 + 1 = 10
is not outside the list and I can print it, but whenever I use del
it gives me error. This is the code that doesn't work:
a = [12,12,34,34,56,11,32,32,11,10,10]
g = len(a)
for i in range(0,g-1):
if a[i] == a[i+1]:
del a[i]
print a
However the reverse logic which is I having value from 10 - 1 works but i from 0 - 9 doesn't work. This one works the reverse logic.
a = [12,12,34,34,56,11,32,32,11,10,10]
g = len(a)
for i in range(g-1,0,-1):
if a[i] == a[i-1]:
del a[i]
print a
Can someone explain why, please?