1
 for i in range(1000000000):
 print(i)

This snippet gives memory error than How to run this loop. Any suggestions on how to run it.Thanks.

>>>MemoryError

UPDATE: Actually I was trying something like this:

arr = []

for i in xrange(1000000000):
    arr.append(i*i)
print(max(set(arr)))

I don't know how to deal with such large numbers.

Hemant
  • 1,961
  • 2
  • 17
  • 27

5 Answers5

4

on python2 this will give an error since range returns a list and in this case your list is too big for memory to hold, try in python3 and it will work. try xrange in python2.

PYA
  • 8,096
  • 3
  • 21
  • 38
4

One way of solving it, even though xrange in most cases is superior, is to use a while loop.

counter = 0
while (counter < 1000000000):
    print i
    counter = counter + 1
klutt
  • 30,332
  • 17
  • 55
  • 95
3

Using xrange instead of range should solve your problem

But what is the difference?

For the most part, they are the exact same. They both generate a list of integers. However, range returns a list object whereas xrange returns a xrange object.

Basically xrange doesn't generate a static list at runtime...instead it generates the values as you need them , which prevents memory error.

Happy coding!

Anish Ghai
  • 74
  • 4
1

The range usually returns an list of values which can be iterated. But, however if you are trying to generate a very large number of values then python Generators are best option as they yield a single value at a time and are being memory efficient to handle large scalable data.

def firstn(n):
    num = 0
    while num < n: 
        yield num
        num += 1

generatorObject = firstn(1000000000)

for i in generatorObject:
    print(i)
Shivam Chaurasia
  • 474
  • 5
  • 21
1

Depends on exactly what you're trying to do. Given that you're using xrange already, I'm going to assume that the issue is with the size of the array that you're creating. If you only want the largest element, you don't need to keep the whole array in memory.

max_ = float("-inf")
for n in xrange(10000000000):
    if n**2 > max_:
        max_ = n**2

print(max_)

Alternatively, if you do want the whole array, you could write the values to a file, then read the file back in chunks.

Batman
  • 8,571
  • 7
  • 41
  • 80