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
🌐
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 Python List comprehension for 1D and 2D empty arrays. Using list comprehension like [[0] * 4 for i in range(3)] creates independent lists for each row.
Published: July 12, 2025
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
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
November 18, 2024 - Create an array using the shape tuple. ... Here, the empty() function creates a 2x3 array.
🌐
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.
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Ways to Initialize an Empty Python Array
January 12, 2026 - If I am processing a list of zip codes from the United States Census Bureau, I use the Python array module to save memory. import array # Creating an empty Python array of integers ('i' represents signed integers) # This is useful for memory-efficient storage of USA Zip Codes zip_codes = array.array('i') print(f"Empty Python Array: {zip_codes}") # Adding a Beverly Hills zip code zip_codes.append(90210) print(f"Updated Python Array: {zip_codes}")
🌐
Quora
quora.com › How-do-I-create-an-empty-array-in-Python
How to create an empty array in Python - Quora
Answer (1 of 6): The closest thing to an array in Python is a list, which is dynamic (the size can change). This is somewhat similar to a C++ [code ]std::vector[/code] or a Java [code ]ArrayList[/code] (if you’re familiar with those languages and data structures). To make an empty list, you ...
🌐
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).

Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › declare an empty array in python
Declare an empty array in Python - CodeSpeedy
September 19, 2023 - You can check out our tutorial on Array creation in Numpy . I have used the empty() function. It takes a number as input which defines the length of the empty array. The other attribute is dtype, which I have defined as an object.
🌐
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.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › numpy tutorial › numpy empty array
NumPy empty array | How does Empty Array Work in NumPy?
May 22, 2023 - To work with arrays, the Python library provides a numpy empty array function. It is used to create a new empty array as per user instruction means giving data type and shape of the array without initializing elements.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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 - To create an empty array with the same shape and data type (dtype) as an existing array, use np.empty_like().
🌐
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.
🌐
Python Examples
pythonexamples.org › python-create-an-empty-array
Python - Create an Empty Array
In Python, you can create an empty array using array() method of array module.
🌐
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.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.0 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
NumPy array creation: numpy.empty() function, example - Return a new array of given shape and type, without initializing entries.