Alternative to standard C "for"

adomas.paltanavicius at gmail.com adomas.paltanavicius at gmail.com
Sat Feb 5 13:32:11 EST 2005


Hi there,

I am quite new to Python, and have a straight & simple question.
In C, there is for (init; cond; advance). We all know that.
In Python there are two ways to loop over i=A..B (numerical.):
1) i = A
   while i<B:
      ...do something...
      i+=STEP
2) for i in range(A, B, STEP):
     ...do something...

First case looks quite nasty, because it's for more complicated
things, not numerical loops. Second is very nice, but with there's
problem. If i do ..in range(1, 100000000).. (what I really need
sometimes), it takes few hundred megs of memory and slows
down. Are there other good ways for this simple problem? Generators?

Adomas




More information about the Python-list mailing list