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
1
  • 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. Commented May 15, 2015 at 2:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@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.
In Python 3, range() creates a neat range object with some very useful optimizations. On top of that, Python 3 lets you print that generator with just print(*test(), sep='\n').
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: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.