Home python How to declare a fixed-length list in python, but not fill it?

How to declare a fixed-length list in python, but not fill it?




For example string

l = list (range (0,1000000000))

Throw MemoryError because the list is populated with numbers from 0 to 999999999 in addition to the declaration.

Answer 1, authority 100%

A fixed-length list is most likely an ARRAY. Then like this:

import numpy as np
MAXSIZE = 1000000
l = np.empty (MAXSIZE, dtype = np.int16)

only it will be filled with “residual garbage”, not “Nothing”.
Nothing showed you in the other answer)
The array implementation in numpy is much more memory efficient than a standard list, but it does not allow storing objects of different types and adding or removing array elements.
By the way – if the type is not specified, then the array will be float

Answer 2, authority 100%

A list in Python contains references to objects. You cannot create a list of a given size without filling it in. Even if all links point to None:

L = [None] * 10 ** 9

you need a place for the links themselves and sooner or later you will get a MemoryError (I am not aware of Python implementations where lists are lazily created). See Python list size and RAM

Alternatively, you can create an empty list:

L = []

and add elements to it as needed (L.append ({}) ). This will not save you from MemoryError if the number of elements is large enough.

If you want to create an array of numbers, then to save memory you can use the array module , which can have a more compact representation. For example, to create an array with a billion zeros:

import array
a = array.array ('i', [0]) * 10 ** 9

This is less memory than a regular list can take, but it will not save you from MemoryError for large enough sizes (as well as numpy.empty (10 ** 9) ). Reserve memory for list in Python?

In order not to require memory, you can use lazy sequences, for example, in Python 3:

R = range (10 ** 9)

The required elements can be created on the fly. Large sizes such as range (10 ** 21) do not result in a MemoryError in this case. You can also create your own classes, for example, the size of the GmtimeOverflowTable is practically unlimited.

Answer 3, authority 67%

import array
# i - integer https://docs.python.org/3/library/array.html
# array & lt; any & gt; cannot be created.
arr = array.array ('i', range (1000000))

Answer 4, authority 67%

This can be done in Python (SciPy) using Sparse Matrices .


import numpy as np
from scipy.sparse import coo_matrix

create a 10 ^ 20 x 1 matrix with 1000 non-zero elements:

row = np.random.randint (10 ** 10, size = 10 ** 3, dtype = np. int64)
col = np.array ([0] * 10 ** 3)
data = np.random.randint (255, size = 10 ** 3)
coo = coo_matrix ((data, (row, col)), shape = (10 ** 20, 1))


In [29]: coo
Out [29]:
& lt; 100000000000000000000x1 sparse matrix of type '& lt; class' numpy.int32 '& gt;'
    with 1000 stored elements in COOrdinate format & gt;
In [30]: coo.shape
Out [30]: (100000000000000000000, 1)

Answer 5, authority 33%

For example like this:
l = [None for i in range (1000000)]

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions