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
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!

๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.2 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.
๐ŸŒ
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 ...
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ numpy empty array with examples
NumPy Empty Array With Examples - Spark By {Examples}
March 27, 2024 - To create a two-dimensional array of empty use the shape of columns and rows as the value to shape parameter. We passed a list of numbers, [5,3] to the shape parameter. This indicates to numpy.empty() that we want to create an empty NumPy array ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.4 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 โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.5.dev0 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.
๐ŸŒ
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)?
June 20, 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).

Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.5.dev0 Manual
Besides creating an array from a sequence of elements, you can easily create an array filled with 0โ€™s: ... Or even an empty array! The function empty creates an array whose initial content is random and depends on the state of the memory.
๐ŸŒ
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 - import numpy as np empty_array = np.array([]) if empty_array.size == 0: print("Array is empty") else: print("Array is not empty") ... To check its emptiness, we can use the .size attribute on it. import numpy as np # Empty 2D array empty_2d_array ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ numpy โ€บ numpy empty function
NumPy Empty Function
March 5, 2015 - import numpy as np # create a 2D array of uninitialized entries array1 = np.empty((3,3)) print('2D Array: \n',array1)
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 160559 โ€บ empty-2d-array
python - Empty 2D Array [SOLVED] | DaniWeb
For heavy numeric work, prefer NumPy arrays for performance and vectorization; initialize once you know dimensions: arr = numpy.zeros((rows, cols)) (NumPy zeros). More on the aliasing gotcha: Python FAQ: multidimensional lists. defaultdict docs: collections.defaultdict. hoe to write a generic code for creating a empty 2D array and dynamically insert values in it.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 76379867 โ€บ python-empty-numpy-2d-array-and-append-value
Python empty numpy 2D array and append value - Stack Overflow
import numpy as np import randrom unknown = random.randint(2, 666) #arr = np.array([np.array([])]) #arr = np.empty((unknown, 0), int) for ch in range (unknown): some_input = random.randint(1, 666) #arr[ch] = np.append((arr[ch], some_input)) #arr[ch] = np.concatenate((arr[ch], some_input)) #arr = np.append((arr, some_input), axis=ch) #arr = np.concatenate((arr, some_input), axis=ch)
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.3 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.
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-empty-array
Create An Empty Array Using NumPy In Python
May 16, 2025 - import numpy as np # Create a 1D array filled with 7 full_array_1d = np.full(5, 7) print("1D Array filled with 7:") print(full_array_1d) # Create a 2D array filled with 3.14 full_array_2d = np.full((3, 4), 3.14) print("\n2D Array filled with 3.14:") print(full_array_2d) Itโ€™s perfect when you need consistent values across an entire array for calculations or masking. Specifying the dtype in array creation functions optimizes memory usage and ensures compatibility with your computations. import numpy as np # Empty int array empty_int = np.empty(5, dtype=int) print("Empty int array:") print(empty_int) # Zeros float32 array zeros_float32 = np.zeros(5, dtype=np.float32) print("\nZeros float32 array:") print(zeros_float32) # Ones boolean array ones_bool = np.ones(5, dtype=bool) print("\nOnes boolean array:") print(ones_bool)
๐ŸŒ
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.html
numpy.empty โ€” NumPy v2.1 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.
๐ŸŒ
Codingem
codingem.com โ€บ home โ€บ numpy how to create an empty array (a complete guide)
NumPy How to Create an Empty Array (A Complete Guide) - codingem.com
July 10, 2025 - To do this, use the numpy.empty() function but specify the shape of the array as a parameter. Then fill in the values to the empty array. For instance, letโ€™s create an empty 2D array that represents a 2 x 3 matrix: