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

🌐
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).

🌐
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. For reproducible behavior, be sure to set each element of the array ...
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.empty.html
numpy.empty — NumPy v1.25 Manual
>>> np.empty([2, 2], dtype=int) array([[-1073741821, -1067949133], [ 496041986, 19249760]]) #uninitialized
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
type(): This built-in Python function tells us the type of the object passed to it. Like in above code it shows that arr is numpy.ndarray type. To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › what-is-the-preferred-method-to-check-for-an-empty-array-in-numpy
What is the preferred method to check for an empty array in NumPy?
March 26, 2026 - The most preferred and efficient method is using the size attribute, which returns the total number of elements in the array ? import numpy as np # Creating an empty array empty_array = np.array([]) # Creating a non-empty array data_array = ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Create an empty array (np.empty, np.empty_like)
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().
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty_like.html
numpy.empty_like — NumPy v2.5 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]])
🌐
Verve AI
vervecopilot.com › interview-questions › why-is-understanding-an-empty-numpy-array-crucial-for-your-next-technical-interview
Why Is Understanding An Empty Numpy Array Crucial For Your Next… · For Your Next Technical Interview · Interview Q&A | Verve AI
Interviewers will test if you can handle boundary cases, and arrays with zero elements or uninitialized values fall squarely into this category. The primary function for creating an uninitialized empty NumPy array is `np.empty()`. ```python import numpy as np
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.array.html
numpy.array — NumPy v2.5 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. ... An array object satisfying the specified requirements. ... 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 | 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 ...
🌐
NumPy
numpy.org › devdocs › user › basics.creation.html
Array creation — NumPy v2.6.dev0 Manual
You can use these methods to create ndarrays or Structured arrays. This document will cover general methods for ndarray creation. NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-initialize-an-empty-array-of-given-length-using-python
How to Initialize an Empty Array of given Length using Python
March 27, 2026 - Use multiplication operator [None] * length for simple empty arrays. For numerical computations, prefer NumPy's empty() function.
🌐
Educative
educative.io › answers › what-is-the-numpyempty-function-in-python
What is the numpy.empty() function in Python?
In Python, the numpy.empty() function is used to return new array of a given shape and type.
🌐
Spark Code Hub
sparkcodehub.com › numpy › basics › empty-array-initialization
Mastering NumPy empty(): High-Performance Array Initialization
np.empty() function is designed to create arrays quickly by allocating memory without initializing the elements, making it one of NumPy’s fastest array creation methods.
🌐
NumPy
numpy.org › doc › stable › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.5 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.