Loops and things

Neil Cerutti horpner at yahoo.com
Fri Dec 14 11:01:24 EST 2007


On 2007-12-14, rocco.rossi at gmail.com <rocco.rossi at gmail.com> wrote:
> I was wondering how and if it's possible to write a loop in python
> which updates two or more variables at a time. For instance, something
> like this in C:
>
> for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
>     printf("i = %d, j = %d\n", i, j);
> }
>
> So that I would get:
>
> i = 0, j = 0
> i = 1, j = 1
> i = 2, j = 2
> ...
> ...
> ...
> i = 9, j = 19
>
> Can this be done in Python?

Yes, assuming you meant to say: 

i = 0, j = 10
i = 0, j = 11
...
i = 9, j = 19

import sys
from itertools import izip

for i, j in izip(xrange(10), xrange(10, 20)):
  sys.stdout.write("i = %d, j = %d\n", (i, j))

-- 
Neil Cerutti
To succeed in the world it is not enough to be stupid, you must also be well-
mannered. --Voltaire



More information about the Python-list mailing list