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.]])
Answer from Stephen Simmons on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.6.dev0 Manual
Return a new array of given shape and type, without initializing entries. ... Shape of the empty array, e.g., (2, 3) or 2.
๐ŸŒ
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 ...
๐ŸŒ
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 - The NumPy version used in this article is as follows. Note that functionality may vary between versions. ... To create an empty array specifying shape and data type (dtype), use np.empty().
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.5 Manual
Return a new array of given shape and type, without initializing entries. ... Shape of the empty array, e.g., (2, 3) or 2.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-empty-array
Create An Empty Array Using NumPy In Python
May 7, 2025 - NumPy arrays have a fixed size. np.append cannot extend one, so it allocates a brand new array and copies everything across, every single iteration. Build a plain Python list and convert once at the end. Lists over-allocate, so appending to them is cheap. If you know the final length in advance, allocate with np.empty(n) up front and assign by index.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ methods โ€บ empty
NumPy empty() | Programiz
August 30, 2023 - Example import numpy as np # create an array of uninitialized values array1 = np.empty(5) print(array1) # Output: [2.23196843e-316 0.00000000e+000 6.94042595e-310 6.94042589e-310 6.94042589e-310] The resultant array has uninitialized or arbitrary ...
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 โ€บ 1.25 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.25 Manual
On the other hand, it requires the user to manually set all the values in the array, and should be used with caution. ... >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy โ€บ how-to-create-an-empty-and-a-full-numpy-array
How to create an empty and a full NumPy array - GeeksforGeeks
September 19, 2025 - Creating arrays is a basic operation in NumPy. Two commonly used types are: Empty array: This array isnโ€™t initialized with any specific values. Itโ€™s like a blank page, ready to be filled with data later.
๐ŸŒ
Delft Stack
delftstack.com โ€บ "delft stack" โ€บ "howto" โ€บ "python numpy howtos" โ€บ "how to create empty numpy array"
How to Create Empty NumPy Array | Delft Stack
March 11, 2025 - import numpy as np # Create an empty NumPy array of shape (3, 4) empty_array_empty = np.empty((3, 4)) print(empty_array_empty) ... In this example, we again create a 3x4 array. However, the values that appear in the output are not guaranteed ...
๐ŸŒ
AskPython
askpython.com โ€บ python-modules โ€บ numpy โ€บ numpy-empty-and-empty_like
NumPy empty and empty_like - AskPython
October 4, 2026 - Pass a shape with a zero dimension, such as np.empty((0, 3)). Check array.size == 0 to test whether the array contains no elements.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_empty_function.htm
Numpy empty() Function
Here, we have created an empty 2D array by passing (3,3) as an argument to the numpy.empty() function โˆ’
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-empty-python
numpy.empty() in Python - GeeksforGeeks
November 29, 2018 - -> shape : Number of rows -> order : C_contiguous or F_contiguous -> dtype : [optional, float(by Default)] Data type of returned array. ... # Python Programming illustrating # numpy.empty method import numpy as geek b = geek.empty(2, dtype = int) print("Matrix b : \n", b) a = geek.empty([2, 2], dtype = int) print("\nMatrix a : \n", a) c = geek.empty([3, 3]) print("\nMatrix c : \n", c) Output :
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.4 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.4 Manual
Return a new array of given shape and type, without initializing entries. ... Shape of the empty array, e.g., (2, 3) or 2.
๐ŸŒ
Python Guides
pythonguides.com โ€บ create-an-empty-array-in-python
Python Empty Array: Create, Declare, Size N and NumPy
January 12, 2026 - For NumPy, use arr.size == 0. Recent NumPy raises ValueError for the truth value of an empty array, which contradicts a lot of older advice that says if not arr is fine.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.1 Manual
Return a new array of given shape and type, without initializing entries. ... Shape of the empty array, e.g., (2, 3) or 2.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ numpy tutorial โ€บ numpy empty
NumPy Empty | Working and Examples of NumPy empty()
March 28, 2023 - This function returns a new empty array having the entries or data that are not initialized but having the array object initialized to none with defined or a given shape and data type parameters. Now let us demonstrate this function in the below simple example: ... import numpy as np print("Program to demonstrate empty function with various ways") print("\n") arr1 = np.empty(4, dtype = int) print("The array with garbage value is as follows") print(arr1) print("\n") arr2 = np.empty([4, 2], dtype = int) print("The multi-dimensional array with garbage value is as follows") print(arr2) print("\n")
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-empty-python
numpy.empty() in Python - GeeksforGeeks
November 29, 2018 - numpy.empty(shape, dtype = float, order = 'C') : Return a new array of given shape and type, with random values.