I have something like this :
#tokens is a list of a few words
for i in xrange(0,len(tokens)):
#some code to modify the contents of token[i]
if tokens[i] == some value:
del tokens[i]
Now if the array has 7 elements, i goes from 0 to 6, and in the middle of the processing if I delete an array element, then the new size becomes 6 but the loop will still run till i=6 and access tokens[6] and throw an error because the new size is 6 i.e. max index is 5.
I guess I can use a while loop with some condition like:
while(i<currMaxIndex)
where I can dynamically change currMaxIndex.
But I was just really curious to know if there was any way to alter i in the for loop itself.
If absolutely MUST 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]