Manipulate index of for loop in Python

Viewed 6361

Is it possible to manipulate the index pointer in a Python for loop?

For example in PHP the following example will print 1 3:

$test = array(1,2,3,4);
for ($i=0; $i < sizeof($test); $i++){
    print $test[$i].' ';
    $i++;
}

However in Python there is no effect when I try to increment the index. For example the following will print all the numbers:

test = ['1', '2', '3', '4']
for i in xrange(len(test)):
  print test[i]
  i=i+1

Is there a way to manipulate the for loop pointer inside the loop so I can implement some complex logic (e.g. go back 2 steps and then forward 3)? I know there may be alternative ways to implement my algorithm (and that's what I do at the moment) but I'd like to know if Python offers this ability.

4 Answers
Related