mercredi 28 juillet 2021

Lopping Through List Index Err [duplicate]

I'm attempting to loop through a list and if list[index] == previous list[index] so == list[index-1], the script should delete both of those elements.

But I'm getting a weird Index Error which in my opinion shouldn't happen:

Here is my code

m = list(input())

for i in range(1, len(m)):
    if m[i] == m[i-1]:
        del m[i]
        del m[i-1]

But it's crashing with the following error

line 4, in <module>
    if m[i] == m[i-1]:
IndexError: list index out of range

Thank you in advance for helping me.

Aucun commentaire:

Enregistrer un commentaire