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
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 โ€บ 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.
Discussions

How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
I can't say I fully followed your problem statement, but you can create an array with a total size of zero if any of the dimensions has size zero: a = np.empty((0, 3)) # Doesn't really matter if you use `empty`, `zeros` or `ones` here Zero-size arrays are the neutral element wrt. concatenation along their zero-size dimension (if that's what you mean by "merging"): b = np.random.uniform(size=(20, 3)) c = np.concatenate([a, b], 0) (c == b).all() # True More on reddit.com
๐ŸŒ r/learnpython
2
2
September 21, 2023
How to fill a matrix using numpy
There's lots of ways to fill arrays. Here are some examples: import numpy as np arr = np.empty((3, 4)) # Value by value for i in range(arr.shape[0]): for j in range(arr.shape[1]): arr[i, j] = i + j # Row by row for i in range(arr.shape[0]): row = range(arr.shape[1]) arr[i, :] = row # Column by column for j in range(arr.shape[1]): row = range(arr.shape[0]) arr[:, j] = row # Or just all at once arr = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Or use one of many built-in functions arr = np.arange(3 * 4).reshape(3, 4) More on reddit.com
๐ŸŒ r/learnpython
3
2
June 18, 2020
Is a (1,n) shaped array not 1D?
No, a (1,n) shaped array has two dimensions. array.reshape(1,-1) asks for a two-dimensional array with a dimension of length 1 and a second dimension as long as required to hold the data. try array.reshape(-1) More on reddit.com
๐ŸŒ r/learnpython
5
4
August 7, 2024
How to convert a 2D index into a 1D index?
Next time you should just try it yourself. Your math is correct. More on reddit.com
๐ŸŒ r/csharp
9
4
April 3, 2024
๐ŸŒ
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 - This article explains how to create an empty array (ndarray) in NumPy. There are two methods available: np.empty(), which allows specifying any shape and data type (dtype), and np.empty_like(), which ...
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ array-creation โ€บ empty.php
NumPy: numpy.empty() function - w3resource
The np.empty() function creates an array without initializing its values, which means that the values of the array are undefined and may vary each time the function is called. In the first example, an empty 1D array of size 2 is created, resulting ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.25 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.25 Manual
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
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.
๐ŸŒ
CodingNomads
codingnomads.com โ€บ arrays-in-numpy-ndarray
Arrays in NumPy: ndarray, np.empty, np.arange, np.linspace
The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. The type of items in the array is specified by a separate datatype object (dtype), one of which is associated with each ndarray. Note: The NumPy dtype (dtype) is always the same for every element in the array.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 โ€บ python-numpy-empty-array
Create An Empty Array Using NumPy In Python
May 7, 2025 - An empty array raises too, and the message names the fix directly: Use array.size > 0. There is more on this in checking whether a NumPy array is empty.
๐ŸŒ
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 - The easiest and most robust way to check if a numpy 1d, 2d, 3d, or multi-dimensional array is empty using np.ndarray.size attribute.
๐ŸŒ
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. Default is numpy.float64. This parameter is optional. # order -> Indicates whether multi-dimensional data should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.
๐ŸŒ
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.
๐ŸŒ
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 โ€บ doc โ€บ 1.18 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.18 Manual
May 24, 2020 - >>> np.empty([2, 2], dtype=int) array([[-1073741821, -1067949133], [ 496041986, 19249760]]) #uninitialized
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.13.0 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v1.13 Manual
Return a new array of given shape and type, without initializing entries. ... empty, unlike zeros, does not set the array values to zero, and may therefore be marginally faster.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can i create a truly empty numpy array which can be merged onto (by a recursive function)?
r/learnpython on Reddit: How can I create a truly empty numpy array which can be merged onto (by a recursive function)?
September 21, 2023 -

I'm kind of stuck conceptually on how to make this happen. I have a recursive method that builds a binary tree, and stores the tree as an instance variable. However, the function is not allowed to return anything, so each recursive call should (according to me) modify in-place the tree instance variable. However, I'm not sure how to set up my instance variable such that all said and done it holds a multidimensional array that represents the tree.

Say I set initialize it as a 1x1 array with element zero as a placeholder. Then as I go about recursing through my tree I can merge to it... but at the end I'm left with a spare [0] element that I don't need. In this case, I'd need some kind of final stop condition and function to remove that unnecessary placeholder stump. I don't think this is possible?

Otherwise, say I initialize the instance variable as None. Then when the first series of recursive calls, it would have to reassign the tree variable to change from None to an ndarray object, but all future calls would have to merge to the array. I don't think this is what the function should be asked to do?

Is there a way to make a truly empty array that I can merge onto? (e.g. np.empty doesn't reallly give an empty array, it gives an array with placeholder values so I'm still left with a useless stump at the end).

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_empty_function.htm
Numpy empty() Function
We can create an n-dimensional NumPy array by passing a tuple of two numeric values as an argument to the numpy.empty() function.