l = range(int(1E9))

Jon Ribbens jon+usenet at unequivocal.co.uk
Sat May 2 20:13:02 EDT 2015


On 2015-05-02, BartC <bc at freeuk.com> wrote:
> On 02/05/2015 22:40, Jon Ribbens wrote:
>> I think the issue is that nobody else here thinks the "original way"
>> of iterating was to use range(), for anything other than extremely
>> small ranges.
>
> What /is/ the way to iterate then?

Other people have already explained that to you several times.
If you want a literal 'for (i = 0; i < x; i++)' loop then it's
'for i in xrange(x)' (from Python 1.0 to Python 2.7), but most
of the time you simply wouldn't be writing code that works that
way.

> I don't have much Python code lying around, but the first couple of 
> files I looked at (not mine), one had this:
>
>    for i in range(7,-1,-1):
>      for j in range(8):
>
> another had:
>
>      for l in range(1,17):
>        for i in range(1, self.bits[l] + 1):
>
>      for i in range(256):
>
> and so on. Plenty of examples.

... of extremely small ranges, just as I said.

> I don't think the small size of the range is a mitigating factor, other 
> than it won't run out of memory. Even a small loop can be executed 
> millions of times.

So what? The overhead of creating a list of 17 integers is trivial
compared to executing the rest of the code. As the man said:
"premature optimization is the root of all evil".



More information about the Python-list mailing list