Running multiple (i.e. normal and reversed) iterators simultaneously in a "for" loop
<p>Haven't found a satisfactory answer so far, hence posting this as a new question. </p>

<p>I have to do the following:</p>

<p>I have a parameter, e.g. <code>test_num = 5</code>.
Now, in a single iteration of a <code>for</code> loop, I want the iterator to run both forwards and backwards simultaneously.</p>

<p>So as an output, i want something like this:</p>

<pre><code>Forward is 0, backward is 5.
Forward is 1, backward is 4.
Forward is 2, backward is 3.
Forward is 3, backward is 2. 
Forward is 4, backward is 1.
</code></pre>

<p>The only thing that i could think up of, is:</p>

<pre><code>test_num = 5
for j in range(test_num):
    for i in range(test_num, 0, -1):
        print "Forward is ", i, ", Backward is ", j
</code></pre>

<p>But this is obviously not the right approach. Is using <code>zip</code> the only option? Because <code>zip</code> only works in case if i'm using the same parameter or in case of two parameters which are equal. I'm looking for something that's flexible enough. </p>

<p>fastest solution:</p>

<pre><code>test_num = 5
for i in range(test_num):
    print("Forward is %d, backward is %d."%(i, test_num-i))
</code></pre>

<p>another fast solution(my solution if you wont use <code>test_num-i</code> expression) :</p>

<pre><code>test_num = 5
for i,j in enumerate(range(test_num,0,-1)):
    print("Forward is %d, backward is %d."%(i, j))
</code></pre>

<p>another solution using zip (if you want use zip! but is slow):</p>

<pre><code>test_num = 5
for i,j in zip(range(test_num), range(test_num, 0, -1)):
    print("Forward is %d, backward is %d."%(i, j))
</code></pre>

<p><strong>benchmarks:</strong></p>

<pre><code>timeit.timeit('for i in range(5):pass', number=10000) # 0.004307041002903134
timeit.timeit('for i,j in enumerate(range(5,0,-1)): pass', number=10000) # 0.007563826999103185
timeit.timeit('for i,j in zip(range(5), range(5, 0, -1)): pass', number=10000) # 0.010275325999828056
</code></pre>

python for-loop iterator