1

I'm running this program to create a iterator but I get a memory error even before it started printing anything.

def test():
        for x in range(10000000000000):
                yield x

for x in test():
        print 'hi'

output:

tutorial@p1980:~/tej$ python itertest.py
Traceback (most recent call last):
  File "itertest.py", line 7, in <module>
    for x in test():
  File "itertest.py", line 4, in test
    for x in range(10000000000000):
MemoryError
user1050619
  • 16,744
  • 58
  • 193
  • 347
  • And what is your question? `range(10000000000000)` creates a pretty big list. If I just run `x = range(10000000000000)`, the process uses up to 56GB of memory before it dies. – Felix Kling May 15 '15 at 02:16

1 Answers1

3

You appear to be using Python 2. In this case, use xrange() instead of range(). The xrange() function returns an object that works like an iterator instead of a list.

In Python 3, range() returns an object that works like an iterator, and does not offer an xrange() function.

Greg Hewgill
  • 828,234
  • 170
  • 1,097
  • 1,237
  • @Shashank: The difference is semantic. Python 2 actually returns an "xrange object", which for the above purpose is functionally identical to an iterator. Anyway, I've edited the answer. – Greg Hewgill May 15 '15 at 02:26
  • In Python 3, [`range()` creates a neat `range` object](http://stackoverflow.com/questions/30081275/why-is-1000000000000000-in-range1000000000000001-so-fast) with some very useful optimizations. On top of that, Python 3 lets you print that generator with just `print(*test(), sep='\n')`. – TigerhawkT3 May 15 '15 at 02:27
  • 3
    Still, it would be better to say that it returns an iterable object. Even if you understand that it doesn't exactly return an iterator, it would be best to avoid confusing others with the term. Python even has a specific error for this: `next(xrange(10))` (or range in Python 3.x) yields: `TypeError: xrange object is not an iterator` – Shashank May 15 '15 at 02:31
  • @Shashank: I've already updated the answer to avoid the misleading terminology. I feel that further explanations of the details of the internal implementation in Python would detract from the answer. Readers may refer to these comments if further detail is desired. – Greg Hewgill May 15 '15 at 02:48