1

I tried it on here but it can not print the long numbers like this

for i in range(1,222222222222222):
    print i

Error:

Traceback (most recent call last):
  File "x.py", line 1, in <module>
    for i in range(1,222222222222222):
MemoryError
svick
  • 236,525
  • 50
  • 385
  • 514
gmarian
  • 2,485
  • 1
  • 13
  • 8

7 Answers7

10

Use xrange.

for i in xrange(1, 222222222222222):
    print i

This function is very similar to range(), but returns an “xrange object” instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously.

4

You are probably using Python2, where range() produces a list of numbers. A list with 222222222222222 elements is quite large, too large for most RAMs.

In contrast to this, xrange() produces an xrange object which can be accessed like a list (indexed, iterated), but doesn't occupy as much space because the values are computed on-demand.

In Python3, range() returns a range object which is quite the same as the xrange object in 2.x.

glglgl
  • 89,107
  • 13
  • 149
  • 217
4

Use xrange instead of range. range generates a list, stores it in the memory and performs the loop on each item. This generates memory errors when dealing with quite big numbers.

xrange is a generator not a list, items are created on the fly, so it does not harm for the memory

I hope this helps

Cobry
  • 4,348
  • 8
  • 33
  • 49
2

Use xrange instead. The range function actually constructs the list in memory, while xrange returns a generator (similar to an iterator), and returns only one number at a time.

for i in xrange(1,222222222222222L):
    print i

See more on the topic here

Community
  • 1
  • 1
Vajk Hermecz
  • 5,413
  • 2
  • 34
  • 25
1

Python creates the range before you start the loop, resulting in a huge amount of memory use/oom error.

you're better off using xrange, it allocates the range in smaller pieces.

for i in xrange(from, to):
    print i
Minion91
  • 1,911
  • 12
  • 19
0

you can also create generator using yield on your own:

def my_gen(start, end, step):
    while start < end:
        start += step
        yield start

for x in my_gen(1, 1000, 2):
    print x
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
-1

Check this example of While Loop

a = 0
while a < 10 :
    a += 1
    print (a)

You can also do the same using "For Loop"

onetoten = range(1,11)
for count in onetoten:
    print (count)
riti
  • 255
  • 2
  • 11
  • 2
    The questionner complains about `range()` not being fit for his purpose and you suggest using `range()` instead? Interesting. – glglgl Nov 09 '12 at 10:18
  • 2
    @glglgl `xrange()` might not work for such large int `222222222222222`, it raises `OverFlowError` on my 32-bit system, see [this](http://stackoverflow.com/questions/2187135/range-and-xrange-for-13-digit-numbers-in-python), so `while-loop` might be a better solution here. – Ashwini Chaudhary Nov 09 '12 at 10:37
  • @AshwiniChaudhary Yes, the first half of the answer is ok, but the 2nd half not. – glglgl Nov 09 '12 at 11:25