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.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. For reproducible behavior, be sure to set each element of the array before reading. ... >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
🌐
GeeksforGeeks
geeksforgeeks.org › python › 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.25 › reference › generated › numpy.empty.html
numpy.empty — NumPy v1.25 Manual
Reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. 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.5.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. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
🌐
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 › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.4 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. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
🌐
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. For reproducible behavior, be sure to set each element of the array before reading. ... >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
🌐
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. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
Find elsewhere
Top answer
1 of 16
611

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
149

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!

🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › empty
Python Numpy empty() - Create Empty Array | Vultr Docs
November 18, 2024 - The numpy.empty() function in Python is a part of the NumPy library, commonly used for generating arrays with uninitialized entries. This method proves useful primarily when you need to allocate an array quickly without immediately populating ...
🌐
Python Guides
pythonguides.com › python-numpy-empty-array
Create An Empty Array Using NumPy In Python
May 16, 2025 - NumPy’s empty() function in Python is the fastest way to create an empty array as it allocates memory without initializing the values.
🌐
Appdividend
appdividend.com › create-and-check-if-a-numpy-array-is-empty
How to Create and Check If a Numpy Array is Empty
July 10, 2025 - For creating a 2D or multidimensional empty array, the better approach is to use the “np.empty()” function. ... import numpy as np arr2d = np.empty((0, 3)) # 0 rows and 3 columns print(arr2d) print(arr2d.size) print(arr2d.shape) # Output: # [] # 0 # (0, 3)
🌐
Programiz
programiz.com › python-programming › numpy › methods › empty
NumPy empty()
like (optional)- reference object to create arrays that are not NumPy arrays · The empty() method returns the array of given shape, order, and datatype filled with arbitrary data. import numpy as np ·
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.empty_like.html
numpy.empty_like — NumPy v2.5.dev0 Manual
Unlike other array creation functions (e.g. zeros_like, ones_like, full_like), empty_like 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. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], # uninitialized [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000], # uninitialized [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.empty.html
numpy.empty — NumPy v1.13 Manual
numpy.empty(shape, dtype=float, order='C')¶ · Return a new array of given shape and type, without initializing entries. See also · empty_like, zeros, ones · Notes · empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty_like.html
numpy.empty_like — NumPy v2.4 Manual
Unlike other array creation functions (e.g. zeros_like, ones_like, full_like), empty_like 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. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], # uninitialized [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000], # uninitialized [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.0 › reference › generated › numpy.empty.html
numpy.empty — NumPy v1.10 Manual
numpy.empty(shape, dtype=float, order='C')¶ · Return a new array of given shape and type, without initializing entries. See also · empty_like, zeros, ones · Notes · empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster.
🌐
Sharp Sight
sharpsight.ai › blog › numpy-empty
How to Use Numpy Empty - Sharp Sight
February 6, 2024 - This tutorial will show you how to use the NumPy empty function to create an empty NumPy array. It explains the syntax of np.empty and gives code examples.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.empty_like.html
numpy.empty_like — NumPy v2.1 Manual
Unlike other array creation functions (e.g. zeros_like, ones_like, full_like), empty_like 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. For reproducible behavior, be sure to set each element of the array before reading. ... >>> import numpy as np >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], # uninitialized [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000], # uninitialized [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])