0

I want to iterate on a list and remove an element if it meets some conditions. But if I remove the element with .pop the list becomes shorter and I get the error : IndexError: list index out of range.

mylist = ["a","a","b","c","d","e"]

for i in range(len(mylist)):
    while mylist[i] == "a":
        mylist.pop(i)
        
print(mylist)

Is it possible to decrease of 1 the range of the for loop whenever I remove an element from a list? How can I solve this? Thank you

Jhon
  • 1
  • 2

0 Answers0