-1

I want to use an array or a list , but with 987654321 items.

The problem is that I get an error , Memoryerror

My code looks as follows:

list = [0]*987654321
for i in range(1, list, 1):
        tableau.append(1)

How can i fix it,thank you

Edit

The purpose that i want to use this huge list is that i want to generate all prime number less than 987654321 , and i'm trying to use the sieve of eratosthenes

user3521250
  • 115
  • 3
  • 14

2 Answers2

2

First, 987654321 is a huge number, it'll take around 8GB of free memory to represent a list of that length filled with 0s. Your system (which appears to be 32-bit and/or has a small amount of RAM) runs out of memory with a list of that size, period; according to this post the theoretical maximum size of a list in a 32-bit CPython implementation is 536870912 elements, or in general: sys.maxsize.

In the current system the program never gets past the first line, and you should rethink your approach, using a file or an external database will probably be a better idea. Also consider running the program in a better machine.

Second, you shouldn't use list as a variable name, that's a built-in function and you should not shadow it. Third, the iteration loop looks wrong (see range()'s documentation to understand the expected parameters), it should be:

tableau = []
for i in xrange(987654321): # assuming Python 2.x
    tableau.append(1)

Notice that there's no need to initialize the list in 0, you can simply append elements to it. Or even simpler, if all the elements are going to be 1, this will work as well (of course, assuming that the system didn't crash because of the huge size):

tableau = [1] * 987654321
Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • That theoretical maximum only applies to 32-bit systems, right? `[0] * 987654321` works just fine on my laptop, consuming a little under 8 Gb of RAM. – Mark Dickinson Aug 24 '14 at 19:36
  • @MarkDickinson that's right, I added that clarification. To be more precise the maximum would be `sys.maxsize`, but available memory will run out long before that – Óscar López Aug 24 '14 at 19:38
0

That's because the list has too many (987654321) elements and your memory is not enough. The code actually raises the exception on the first line. Just start with empty list and add items into it. If you really have to have that many items in the list (which in most cases is not necessary) you should think of a better way to store that many information. The simplest solution I can think of is to write the elements in a file instead directly to the memory.

artur.balabanov
  • 181
  • 1
  • 8