Build a Python list and convert that to a Numpy array. That takes amortized O(1) time per append + O(n) for the conversion to array, for a total of O(n).

Use with AI    a = []
    for x in y:
        a.append(x)
    a = np.array(a)
Answer from Fred Foo on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.5 Manual
Unlike other array creation functions (e.g. zeros, ones, full), empty does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary.
🌐
w3resource
w3resource.com › numpy › array-creation › empty.php
NumPy: numpy.empty() function - w3resource
In the second example, an empty 2D array of size (2, 2) is created with the specified data type float. The resulting array contains four undefined floating-point values. The values shown in the output are also machine-dependent and may vary each time the function is called. ... import numpy as np # Define a custom data type dt = np.dtype([('Employee Name:', np.str_, 16), ('Age:', np.int32), ('Salary:', np.float64)]) # Create an empty array with the custom data type employee = np.empty((2, 3), dtype=dt) # Print the array print(employee)
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Create an empty array (np.empty, np.empty_like) | note.nkmk.me
January 22, 2024 - If the final number of elements is already known, it is better to create an array of the necessary size using np.empty() or np.empty_like() and then assign values. NumPy: Get and set values in an array using various indexing
Top answer
1 of 16
612

That is the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. To append rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly.

Instead of appending rows, allocate a suitably sized array, and then assign to it row-by-row:

>>> import numpy as np

>>> a = np.zeros(shape=(3, 2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

>>> a[0] = [1, 2]
>>> a[1] = [3, 4]
>>> a[2] = [5, 6]

>>> a
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
2 of 16
150

A NumPy array is a very different data structure from a list and is designed to be used in different ways. Your use of hstack is potentially very inefficient... every time you call it, all the data in the existing array is copied into a new one. (The append function will have the same issue.) If you want to build up your matrix one column at a time, you might be best off to keep it in a list until it is finished, and only then convert it into an array.

e.g.


mylist = []
for item in data:
    mylist.append(item)
mat = numpy.array(mylist)

item can be a list, an array or any iterable, as long as each item has the same number of elements.
In this particular case (data is some iterable holding the matrix columns) you can simply use


mat = numpy.array(data)

(Also note that using list as a variable name is probably not good practice since it masks the built-in type by that name, which can lead to bugs.)

EDIT:

If for some reason you really do want to create an empty array, you can just use numpy.array([]), but this is rarely useful!

🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.2 Manual
Unlike other array creation functions (e.g. zeros, ones, full), empty does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary.
Find elsewhere
🌐
IncludeHelp
includehelp.com › python › initialise-numpy-array-of-unknown-length.aspx
Python - Initialise numpy array of unknown length
To initialise numpy array of unknown length, we can build a Python list and convert that to a Numpy array. That takes amortized O(1) time per append + O(n) for the conversion to the array, for a total of O(n). ... # Import numpy import numpy as np # Creating a list l = [] # Appending elements ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.1 Manual
Unlike other array creation functions (e.g. zeros, ones, full), empty does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary.
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Python Empty Array: Create, Declare, Size N and NumPy
January 12, 2026 - For a list, if not items:. For a NumPy array, use arr.size == 0, because the truth value of an empty NumPy array raises ValueError.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
In this example, we are using a Python loop for 1D and 2D empty arrays. ... In this method, we are using the Numpy module to generate an empty matrix of 1D and 2D sizes using np.empty().
Published: July 12, 2025
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.6.dev0 Manual
Unlike other array creation functions (e.g. zeros, ones, full), empty does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary.
🌐
Educative
educative.io › answers › how-to-create-an-empty-numpy-array
How to create an empty NumPy array
numpy.zeros(shape, dtype=float, order='C') numpy.empty(shape, dtype=float, order='C') # Shape -> Shape of the new array, e.g., (2, 3) or 2. # dtype -> The desired data-type for the array,e.g., numpy.int8.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.3 Manual
Unlike other array creation functions (e.g. zeros, ones, full), empty does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary.
🌐
YouTube
youtube.com › how to fix your computer
PYTHON : Initialise numpy array of unknown length - YouTube
PYTHON : Initialise numpy array of unknown length [ Gift : Animated Search Engine : https://www.hows.tech/p/recommended.html ] PYTHON : Initialise numpy arr...
Published: December 6, 2021
Views: 187
🌐
Python Help
pythonhelp.org › numpy › how-to-create-an-empty-numpy-array
Creating an Empty NumPy Array
Before diving into the steps, let’s quickly explore why you might need an empty NumPy array: Initialization: When working with complex numerical computations that involve arrays of different sizes or shapes, starting with empty arrays ensures efficient memory allocation and avoids unnecessary computations.
🌐
AskPython
askpython.com › python-modules › numpy › numpy-empty-and-empty_like
NumPy empty and empty_like - AskPython
October 4, 2026 - In a shape of (2, 3), the dimensions request two rows and three columns, and dtype selects the representation for each element. ... Pass the intended dimensions to np.empty, then assign every element before you inspect or calculate with the array.