Python 'for' loop is memory inefficient

Hendrik van Rooyen hendrik at microcorp.co.za
Sat Aug 15 11:21:12 CEST 2009


On Saturday 15 August 2009 03:25:45 Dr. Phillip M. Feldman wrote:

> 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.

There is.

Use xrange instead of range, and try again.

And while you are about it, you may as well teach them that it is much better 
to do a multiplication than a division.

-Hendrik




More information about the Python-list mailing list