>
> It seems as though Python is actually expanding range(2,n) into a list of
> numbers, even though this is incredibly wasteful of memory. There should be
> a looping mechanism that generates the index variable values incrementally
> as they are needed.


This has nothing to do with Python's for loop (and saying the loop construct
itself is memory inefficient makes me blink in confusion): but you've
isolated the problem precisely all on your own. range() is defined as
returning a list. Thus, it constructs the list all at once.

xrange() returns an iterator, and generates the values on the fly.

In Python 3, this was changed so range returns an iterator, and to get a
list you do list(range(2,n)).

--S
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to