Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 2 index variable in a single for loop in python

In C language we can use two index variable in a single for loop like below.

for (i = 0, j = 0; i < i_max && j < j_max; i++, j++)

Can some one tell how to do this in Python ?

like image 569
rashok Avatar asked Feb 25 '26 04:02

rashok


1 Answers

With zip we can achieve this.

>>> i_max = 5
>>> j_max = 7
>>> for i, j in zip(range(0, i_max), range(0, j_max)):
...     print str(i) + ", " + str(j)
...
0, 0
1, 1
2, 2
3, 3
4, 4
like image 92
rashok Avatar answered Feb 27 '26 17:02

rashok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!