Python MemoryError on large array

Viewed 12991

This is the python script that I'm trying to run:

n = 50000000000 ##50 billion 
b = [0]*n
for x in range(0,n):
    b[x] = random.randint(1,899999)

... But the output I'm getting is:

E:\python\> python sort.py
Traceback (most recent call last):
  File "E:\python\sort.py", line 8, in <module>
    b = [0]*n
MemoryError

So, what do I do now?

3 Answers

Since other people already answered your question here's a quick tip when dealing with big numbers: you can use "_" to separate the digits of your numbers as you wish:

n = 50_000_000_000

is the same as

n = 50000000000

but the former is much easier on the eyes

One other possibility is to increase you computers vitual memory. It helped me in my code. I had a max 3000MB virtual memory, when I increased it to 5000MB the memory error was gone.

Related