For example, the string

l=list(range(0,1000000000)) 

MemoryError error, because the list, in addition to the declaration, is filled with numbers from 0 to 999999999.

  • 2
    The counter question - why? If you tell more about your task, you can be prompted by other solutions ... PS You should not use C / C ++ templates in Python - MaxU
  • "Here in c ++, for example, this is done easily," however, the result does not compile a bit :) ideone.com/Or9N3C - D-side
  • 2
    You cannot create a list of fixed length without filling it, neither in python, nor c ++, nor in C, nor in any other language. The list will necessarily be filled with at least something - for example, zero, null or None. And in any of these cases, you risk getting a MemoryError or its equivalent in any language. - andreymal
  • int arr [10]; also allocates memory for the array. - Jenyay

5 answers 5

A fixed length list is most likely a MASSIF. Then so:

 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 another answer)
The implementation of the array in numpy much more economical in terms of memory consumption than the standard list, but it does not allow storing objects of different types and adding or removing elements of the array.
By the way - if the type is not specified, then the float array will be

  • one
    Still, on large sizes MemoryError will be - Xander
  • @Alexander, to solve this problem, there is distributed data storage on several servers, SPARK in the end. As you know, memory is physically limited, but the natural number is not. (: There is always an "too big" array size - Vasyl Kolomiets
  • 2
    It is surprising that the author also accepted this answer, too, because he does not solve any of the problems identified in the question (in which, by the way, there were no changes) - andreymal
  • @andreymal - I gave an exact analogue of int arr[10]; for Python. This is what the author wanted. - Vasyl Kolomiets
  • one
    @KonstantinL This answer also leads to MemoryError (maybe with a slight delay. For example, on my phone numpy.empty(10**9) causes MemoryError - jfs

The list in Python contains references to objects. You cannot create a list of a given size without filling it. 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 don’t know Python implementations where lists are lazily created). See Python list size and RAM size.

Alternatively, you can create an empty list:

  L = [] 

and add elements to it as necessary ( L.append({}) ). This will not save MemoryError with a sufficiently large number of elements.

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, 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 occupy, but it will not save memory 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 necessary elements on the fly can be created. 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.

  • @ Maxu can be. sparse matrix are specialized type classes, GmtimeOverflowTable - for specific tasks sharpened (narrow purpose). - jfs
 import array # i - integer https://docs.python.org/3/library/array.html # array<any> создать нельзя. arr = array.array('i', range(1000000)) 

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

    Example:

     import numpy as np from scipy.sparse import coo_matrix 

    create a matrix of dimension 10^20 x 1 with 1000 nonzero 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)) 

    result:

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

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

      • ']' add in the end. but as a result, the entire array will be filled None - de_frag
      • @AlexanderPushkarev, what's wrong with None? - Qwertiy
      • @Qwertiy with None is all right. The question was about declaring an array without initializing elements of the type int ar [10]. Jfs wrote about it later. - de_frag