Python 2.7 range Function provokes a Memory Error

Jon Ribbens jon+usenet at unequivocal.eu
Thu Mar 2 06:29:53 EST 2023


On 2023-03-02, Stephen Tucker <stephen_tucker at sil.org> wrote:
> The range function in Python 2.7 (and yes, I know that it is now
> superseded), provokes a Memory Error when asked to deiliver a very long
> list of values.
>
> I assume that this is because the function produces a list which it then
> iterates through.
>
> 1. Does the  range  function in Python 3.x behave the same way?

No, in Python 3 it is an iterator which produces the next number in the
sequence each time.

> 2. Is there any equivalent way that behaves more like a  for loop (that is,
> without producing a list)?

Yes, 'xrange' in Python 2 behaves like 'range' in Python 3.


More information about the Python-list mailing list