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.

Answer from hpaulj on Stack Overflow
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]
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › how-to-append-a-numpy-array-to-an-empty-array-in-python
How to append a NumPy array to an empty array in Python - GeeksforGeeks
July 23, 2025 - import numpy as np l = np.array([]) l = np.append(l, np.array(['G', 'F', 'G'])) l = np.append(l, np.array(['G', 'F', 'G'])) print(l) ... Here we are creating an empty array and then appending an empty row in it to see if there is any difference.
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!

🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-119.php
Python NumPy: Add a new row to an empty numpy array - w3resource
# Importing the NumPy library and aliasing it as 'np' import numpy as np # Creating an empty NumPy array with shape (0, 3) of integers arr = np.empty((0, 3), int) # Printing a message indicating an empty array will be displayed print("Empty ...
🌐
TestMu AI Community
community.testmuai.com › ask a question
How do I create an empty NumPy array and append items to it? - TestMu AI Community
October 20, 2025 - I want to create an empty array and add items to it one at a time, similar to Python lists: xs = [] for item in data: xs.append(item) Can I use this approach with NumPy arrays, or is there a better way to create em…
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › empty
Python Numpy empty() - Create Empty Array | Vultr Docs
November 18, 2024 - Here, the empty() function creates a 2x3 array. The values in the array are uninitialized. Set the dtype parameter to define the type of array elements.
🌐
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).

Find elsewhere
🌐
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().
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python append element to array
Python Append Element to Array - Spark By {Examples}
May 31, 2024 - For example, you create an empty ... 10, 15]) To append elements to a NumPy array in Python, you can use the append() function provided by the NumPy module....
🌐
Delft Stack
delftstack.com › home › howto › numpy › python numpy empty array append
How to Append to Empty Array in NumPy | Delft Stack
February 2, 2024 - In this example, we have two new rows: [1, 2, 3] and [4, 5, 6]. To add all the rows from new_rows to the rows_list, we use the extend() method of the list. This operation efficiently appends all elements from new_rows to rows_list.
🌐
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
In Python, the multiplication operator will help concate two different values and initialize an empty array of the given length. In the following example, start the program by setting the length value in the variable l. Then use the multiplication operator between ?[none]' and ?l' to create the empty array of a given length in the variable arr.
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-13.php
NumPy: Create an empty and a full array - w3resource
# Importing the NumPy library with an alias 'np' import numpy as np # Creating an empty array of shape (3, 4) using np.empty() x = np.empty((3, 4)) # Printing the empty array 'x' print(x) # Creating a filled array of shape (3, 3) with all elements as 6 using np.full() y = np.full((3, 3), 6) # Printing the filled array 'y' print(y) ... [[ 6.93643969e-310 8.76783124e-317 6.93643881e-310 6.79038654e-31 3] [ 2.22809558e-312 2.14321575e-312 2.35541533e-312 2.42092166e-32 2] [ 7.46824097e-317 9.08479214e-317 2.46151512e-312 2.41907520e-31 2]] [[6 6 6] [6 6 6] [6 6 6]] ... x = np.append(x, [[40, 50, 60], [70, 80, 90]]): The np.append() function is used to append two lists of three elements each to the original list ‘x’. The resulting object is converted into a NumPy array and stored back into ‘x’.
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Create An Empty Array In Python
December 23, 2024 - For example, empty_list = [] creates an empty list that can later be populated with elements. ... In Python, an array is a data structure that can hold multiple values of the same type.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.4 Manual
Return a new array of given shape and type, without initializing entries. ... Shape of the empty array, e.g., (2, 3) or 2. ... Desired output data-type for the array, e.g, numpy.int8. Default is numpy.float64. order{‘C’, ‘F’}, optional, default: ‘C’ · Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. ... The device on which to place the created array.
🌐
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).
🌐
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