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 › 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.
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
Help with matrices (without using numpy)
If A is a matrix (2D list), len(A) will be the # of rows, and len(a row) will be the # of columns. Use that to get (and compare) dimensions. AB(i,j) = (row i of A) dot (col j of B) Suppose my two matrices (A and B) are [[1,2], [[1,2,3], [3,4] and [4,5,6]] and you want the (0-based indexing) entry in row 1, col 2 that comes from [3,4] dot [3,6]. What do the indices of A look like in that row? What do the indices of B look like in that column? More on reddit.com
🌐 r/learnpython
6
8
February 19, 2021
several lists into one 2d matrix
🌐 r/learnpython
11
0
August 15, 2023
Using numpy.append() without losing array dimensions? [Mac, 2.7]
By default numpy.append flattens both arrays. I would use numpy.dstack and just stack all the arrays on top of one another. You will get a size of (101,6,i) where i is the ith array stacked. import numpy as np a = np.arange(9).reshape(3,3) b = np.arange(9,18).reshape(3,3) c = np.arange(18,27).reshape(3,3) d = np.arange(27,36).reshape(3,3) final = np.dstack((a, b, c, d)) print final.shape print np.array_equal(final[:,:,0], a) print np.array_equal(final[:,:,1], b) print np.array_equal(final[:,:,2], c) print np.array_equal(final[:,:,3], d) The other option is to initialize final_arr to be the correct size first. Then you just index the new arrays into their proper positions. More on reddit.com
🌐 r/learnpython
6
2
February 6, 2014
🌐
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 each time the function is called. ... import numpy as np # Define a custom data type dt = np.dtype([('Employee Name:', np.str_, 16), ('Age:', np.int32), ('Salary:', np.float64)]) # Create an empty array with the custom data type employee = np.empty((2, 3), dtype=dt) # Print the array print(employee)
🌐
thisPointer
thispointer.com › home › numpy › create an empty 2d numpy array / matrix and append rows or columns in python
Create an empty 2D Numpy Array / matrix and append rows or columns in python - thisPointer
March 29, 2020 - To add multiple columns to an 2D Numpy array, combine the columns in a same shape numpy array and then append it, # Create an empty 2D numpy array with 4 rows and 0 column empty_array = np.empty((4, 0), int) column_list_2 = np.array([[16, 26, 36, 46], [17, 27, 37, 47]]) # Append list as a column to the 2D Numpy array empty_array = np.append(empty_array, column_list_2.transpose(), axis=1) print('2D Numpy array:') print(empty_array)
🌐
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)
🌐
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:
Find elsewhere
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 160559 › empty-2d-array
python - Empty 2D Array [SOLVED] | DaniWeb
December 3, 2008 - 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 ...
🌐
Sharp Sight
sharpsight.ai › blog › numpy-empty
How to Use Numpy Empty - Sharp Sight
February 6, 2024 - Remember in the review of NumPy earlier in this tutorial, I explained that NumPy arrays have a shape. The shape is essentially the number of rows and the number of columns (if you have a 2D array). When you create an array with np.empty, you need to specify the exact shape of the output by using the shape parameter.
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
NumPy is used to work with arrays. The array object in NumPy is called ndarray.
🌐
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.
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Ways to Initialize an Empty Python Array
January 12, 2026 - For instance, if I am tracking the monthly temperatures for 5 major US cities over 12 months, I create an empty 2D Python array. import numpy as np # Creating an empty 2D Python array (5 cities x 12 months) # We use np.empty which is faster than np.zeros because it doesn't initialize values city_temps = np.empty((5, 12)) print(f"Shape of Python Array: {city_temps.shape}") print("The array contains uninitialized data (random memory values).")
🌐
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.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy empty array with examples
NumPy Empty Array With Examples - Spark By {Examples}
March 27, 2024 - NumPy empty() array function in Python is used to create a new array of given shapes and types, without initializing entries. This function takes three
🌐
Quora
quora.com › How-do-you-create-an-empty-multidimensional-array-in-Python
How to create an empty multidimensional array in Python - Quora
Answer (1 of 5): You can’t - a multidimensional list (not array) in Python is a list of lists. if the top level list is empty then it isn’t multidimensional - it is an empty list. if the list on the next level down are empty then you have a list which is N by zero - hardly multi-dimensional.
🌐
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().
🌐
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).

🌐
Programiz
programiz.com › python-programming › numpy › methods › empty
NumPy empty()
# create an int array of arbitrary ... If unspecified, the default dtype is float. import numpy as np · # create a 2D array of uninitialized entries array1 = np.empty((2, 3)) print(2D Array: \n',array1) Output ·...
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python | Sentry
June 15, 2023 - We can create a 2D array by passing a tuple to the zeros function. The following code will create a 3-by-3 2D array with all values set to zero: import numpy matrix = numpy.zeros((3,3)) # array([[0., 0., 0.], # [0., 0., 0.], # [0., 0., 0.]])