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 โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.1 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.6.dev0 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.5 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.3 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
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 - For more information about data types (dtype) in NumPy, refer to the following article. ... To create an empty array with the same shape and data type (dtype) as an existing array, use np.empty_like().
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ array-creation โ€บ empty.php
NumPy: numpy.empty() function - w3resource
The numpy.empty() function is used to create a new array of given shape and type, without initializing entries.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.2 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
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!

Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.25 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.25 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.4 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.4 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
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 an empty array is useful when you need a placeholder for future data that will be populated later. It allocates space without initializing it, which can be efficient in terms of performance. Use np.empty() function. Specify the shape ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.0 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0. ... Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ numpy โ€บ empty()
Python Numpy empty() - Create Empty Array | Vultr Docs
November 18, 2024 - Use numpy.empty() to create an array. ... This code initializes an array of length 3 with indeterminate values. The contents of the array will be whatever values happen to already be at those memory locations. Define the desired shape in a tuple.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.17 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.17 Manual
Return a new array of given shape and type, without initializing entries. ... Return an empty array with shape and type of input.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-create-an-empty-numpy-array
How to create an empty NumPy array
Both of these methods differ slightly, as shown below: The syntax for using numpy.zeros and numpy.empty is shown below: numpy.zeros(shape, dtype=float, order='C') numpy.empty(shape, dtype=float, order='C') # Shape -> Shape of the new array, e.g., ...
๐ŸŒ
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.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.16 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.16 Manual
February 18, 2020 - Return a new array of given shape and type, without initializing entries. ... Return an empty array with shape and type of input.
๐ŸŒ
AskPython
askpython.com โ€บ python-modules โ€บ numpy โ€บ numpy-empty-and-empty_like
NumPy empty and empty_like - AskPython
October 4, 2026 - NumPyโ€™s empty functions allocate an array without setting its numeric entries to useful values. โ€œEmptyโ€ here means uninitialized storage, not an array with zero elements. Use np.empty(shape) when you choose the dimensions yourself.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.16 โ€บ reference โ€บ generated โ€บ numpy.ma.empty.html
numpy.ma.empty โ€” NumPy v1.16 Manual
February 18, 2020 - Return a new array of given shape and type, without initializing entries. ... Return an empty array with shape and type of input.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ routines.array-creation.html
Array creation routines โ€” NumPy v2.5 Manual
empty(shape[, dtype, order, device, like]) ยท Return a new array of given shape and type, without initializing entries