IndexError: list index out of range
I am looking for doubled strings in a list and want to remove if there is a repetition. So I use if statement for checking if a element in a list equals to the next and then remove if ture. But unexpectedly when I use del list[i] code returns error.
for i in range(len(formatted_text)-1):
if(formatted_text[i] == formatted_text[i+1]):
# formatted_text[i+1] = ''
print(len(formatted_text)) # it is equal to 11
print(i) # the repetition is at 4 and 5 element
del formatted_text[i] # removal instruction
I expect it to execute normally, don't know where the problem is. When i check it without loop it works normally.
EDIT: I managed to solve this problem. Unfortunately (len(formatted_text)-2, 0 ,-1) doesn't work if the repetition is at the beginning of a list. Here is the code that work's just fine and is friendly:
for i in range(len(formatted_text)-1):
if(formatted_text[i] == formatted_text[i+1]):
formatted_text[i+1] = ''
formatted_text.remove('')