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. Each iteration in the list comprehension creates a new list object, which ensures that changes to one row will not affect others. ... # initialize the spaces with 0’s with # the help of list comprehensions a = [0 for x in range(10)] print(a) b = [[0] * 4 for i in range(3)] print(b)
Published   July 12, 2025
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!

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
June 21, 2023
python - How to create an empty array and append it? - Stack Overflow
The issue is that the line x = empty((2, 2), int) is creating a 2D array. More on stackoverflow.com
🌐 stackoverflow.com
Why does numpy.empty put numbers on the order of 1^9 or 1^(-300) in the array?
It's allocating the memory without initializing. The memory contains whatever was already there, which could be bits of a program or a string or anything at all. Interpreting those random bit patterns as numbers, it's not surprising they might happen to correspond to very large or very small exponents. So it's not "using" any values. That's just the numeric value that's displayed when the array element happened to be the 47,042-th pixel in the picture of somebody's cat. More on reddit.com
🌐 r/learnpython
6
1
August 4, 2022
Trying to fill an empty numpy array with data
That looks like a csv file. Just use numpy's genfromtext function to read it. arr = np.genfromtxt(filename, dtype=int, delimiter=',') More on reddit.com
🌐 r/learnpython
26
2
December 7, 2016
🌐
TutorialsPoint
tutorialspoint.com › how-to-initialize-an-empty-array-of-given-length-using-python
How to Initialize an Empty Array of given Length using Python
August 14, 2023 - The empty array of a given length: [None, None, None, None, None] The program uses built?in method empty() that follow the numpy module to initialize the empty array with given length.
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › empty
Python Numpy empty() - Create Empty Array | Vultr Docs
November 18, 2024 - Use numpy.empty() to create an array. ... This code initializes an array of length 3 with indeterminate values.
🌐
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.
🌐
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 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).

🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Create an empty array (np.empty, np.empty_like)
January 22, 2024 - To create an empty array specifying shape and data type (dtype), use np.empty(). ... Pass the shape as the first argument. A scalar results in a one-dimensional array, and a tuple results in a multi-dimensional array.
Find elsewhere
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › python-empty-list
A Comprehensive Guide to Python Empty Lists | DataCamp
February 2, 2024 - Here we pick up the example from the previous section and insert ’Truly’ between ’Python’ and ’Rocks!’. Note that Python uses zero-based indexing, which means that the index of the first element of the list is 0. Check out the example for more details: # Initialize an empty list my_list = [] # Append elements my_list.append('Python') my_list.append('Rocks!') my_list.append('It’s easy to learn!') # `my_list` is now ['Python', 'Rocks!', 'It’s easy to learn!'] # Showcase zero-based indexing in Python my_list[0] # Returns 'Python' my_list[1] # Returns 'Rocks' my_list[2] # Returns 'It's easy to learn!'
🌐
Quora
quora.com › How-do-I-create-an-empty-array-in-Python
How to create an empty array in Python - Quora
Answer (1 of 7): 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).
🌐
Quora
quora.com › How-do-you-create-a-null-array-in-Python
How to create a null array in Python - Quora
Answer (1 of 3): Python itself does not have arrays, it has lists or tuples. Lists are mutable in that you can add items to and remove items from it. Tuples are immutable in the sense that you can not change the elements it holds or add or remove elements. You can create an empty list calling lis...
Top answer
1 of 4
3

I suspect you are trying to replicate this working list code:

In [56]: x = []                                                                 
In [57]: x.append([1,2])                                                        
In [58]: x                                                                      
Out[58]: [[1, 2]]
In [59]: np.array(x)                                                            
Out[59]: array([[1, 2]])

But with arrays:

In [53]: x = np.empty((2,2),int)                                                
In [54]: x                                                                      
Out[54]: 
array([[73096208, 10273248],
       [       2,       -1]])

Despite the name, the np.empty array is NOT a close of the empty list. It has 4 elements, the shape that you specified.

In [55]: np.append(x, np.array([1,2]), axis=0)                                  
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-64dd8e7900e3> in <module>
----> 1 np.append(x, np.array([1,2]), axis=0)

<__array_function__ internals> in append(*args, **kwargs)

/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in append(arr, values, axis)
   4691         values = ravel(values)
   4692         axis = arr.ndim-1
-> 4693     return concatenate((arr, values), axis=axis)
   4694 
   4695 

<__array_function__ internals> in concatenate(*args, **kwargs)

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

Note that np.append has passed the task on to np.concatenate. With the axis parameter, that's all this append does. It is NOT a list append clone.

np.concatenate demands consistency in the dimensions of its inputs. One is (2,2), the other (2,). Mismatched dimensions.

np.append is a dangerous function, and not that useful even when used correctly. np.concatenate (and the various stack) functions are useful. But you need to pay attention to shapes. And don't use them iteratively. List append is more efficient for that.

When you got this error, did you look up the np.append, np.empty (and np.concatenate) functions? Read and understand the docs? In the long run SO questions aren't a substitute for reading the documentation.

2 of 4
2

You can create empty list by []. In order to add new item use append. For add other list use extend.

x = [1, 2, 3]
x.append(4)
x.extend([5, 6])

print(x) 
# [1, 2, 3, 4, 5, 6]
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
🌐
AskPython
askpython.com › home › 3 ways to initialize a python array
3 ways to initialize a Python Array - AskPython
January 16, 2024 - Python NumPy module can be used to create arrays and manipulate the data in it efficiently. The numpy.empty() function creates an array of a specified size with a default value = ‘None’.
🌐
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 arguments, we can customize the specific data type and order by passing these parameters.
🌐
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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › how-to-create-an-empty-and-a-full-numpy-array
How to create an empty and a full NumPy array - GeeksforGeeks
September 19, 2025 - Python · JavaScript · Data Science · Machine Learning · Courses · Linux · DevOps · Last Updated : 19 Sep, 2025 · Creating arrays is a basic operation in NumPy. Two commonly used types are: Empty array: This array isn’t initialized with any specific values.
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element that is a 4D array. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Ways to Initialize an Empty Python Array
January 12, 2026 - To create an empty array in Python using lists, simply initialize an empty list with square brackets.