I have something like this:
#tokens is a list of a few words for i in xrange(0,len(tokens)):
Now, if the array has 7 elements, I go from 0 to 6, and in the middle of processing, if I delete the element of the array, the new size becomes 6, but the loop will run until i = 6 and access the tokens [6] and give an error, because that the new size is 6, i.e. maximum index is 5.
I think I can use a while loop with some condition like:
while(i<currMaxIndex)
where I can dynamically change currMaxIndex.
But I was just curious to know if there is a way I can change in the for loop.
If this SHOULD NOT know, this is my code:
for i in xrange(0,len(tokens)): tokens[i]=tokens[i].translate(string.maketrans("",""),string.punctuation) if tokens[i]=='': del tokens[i]
source share