The way to "start" the array that you want is:

arr = np.empty((0,3), int)

Which is an empty array but it has the proper dimensionality.

>>> arr
array([], shape=(0, 3), dtype=int64)

Then be sure to append along axis 0:

arr = np.append(arr, np.array([[1,2,3]]), axis=0)
arr = np.append(arr, np.array([[4,5,6]]), axis=0)

But, @jonrsharpe is right. In fact, if you're going to be appending in a loop, it would be much faster to append to a list as in your first example, then convert to a numpy array at the end, since you're really not using numpy as intended during the loop:

In [210]: %%timeit
   .....: l = []
   .....: for i in xrange(1000):
   .....:     l.append([3*i+1,3*i+2,3*i+3])
   .....: l = np.asarray(l)
   .....: 
1000 loops, best of 3: 1.18 ms per loop

In [211]: %%timeit
   .....: a = np.empty((0,3), int)
   .....: for i in xrange(1000):
   .....:     a = np.append(a, 3*i+np.array([[1,2,3]]), 0)
   .....: 
100 loops, best of 3: 18.5 ms per loop

In [214]: np.allclose(a, l)
Out[214]: True

The numpythonic way to do it depends on your application, but it would be more like:

In [220]: timeit n = np.arange(1,3001).reshape(1000,3)
100000 loops, best of 3: 5.93 µs per loop

In [221]: np.allclose(a, n)
Out[221]: True
Answer from askewchan 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!

🌐
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 - Here we are creating an empty array and then appending it to the array. ... 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)
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.append.html
numpy.append — NumPy v2.6.dev0 Manual
>>> a = np.array([1, 2], dtype=np.int_) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64 · Default dtype for empty ndarrays is float64 thus making the output of dtype float64 when appended with dtype int64 · Go BackOpen In Tab ·
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-119.php
Python NumPy: Add a new row to an empty numpy array - w3resource
August 29, 2025 - # 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 array:") # Displaying the empty array print(arr) # Appending two new arrays to the 'arr' array vertically along axis 0 arr = np.append(arr, np.array([[10, 20, 30]]), axis=0) arr = np.append(arr, np.array([[40, 50, 60]]), axis=0) # Printing a message indicating that two new arrays are added to the original array print("After adding two new arrays:") # Displaying the updated 'arr' array after adding two new arrays print(arr)
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python numpy howtos" › "how to append to empty array in numpy"
How to Append to Empty Array in NumPy | Delft Stack
February 2, 2024 - These can be in the form of a single array, a list of arrays, or even scalar values. axis: The axis parameter specifies along which axis the new rows should be appended. If this parameter is not explicitly specified, NumPy will attempt to infer the axis automatically. Let’s walk through a simple example to demonstrate how to use np.append(): import numpy as np empty_array = np.array([]) # Create an empty array new_row = np.array([1, 2, 3]) # Create a new row updated_array = np.append(empty_array, new_row, axis=0) print(updated_array)
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]
Find elsewhere
Top answer
1 of 6
71

I might understand the question incorrectly, but if you want to declare an array of a certain shape but with nothing inside, the following might be helpful:

Initialise empty array:

>>> a = np.zeros((0,3)) #or np.empty((0,3)) or np.array([]).reshape(0,3)
>>> a
array([], shape=(0, 3), dtype=float64)

Now you can use this array to append rows of similar shape to it. Remember that a numpy array is immutable, so a new array is created for each iteration:

>>> for i in range(3):
...     a = np.vstack([a, [i,i,i]])
...
>>> a
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 2.,  2.,  2.]])

np.vstack and np.hstack is the most common method for combining numpy arrays, but coming from Matlab I prefer np.r_ and np.c_:

Concatenate 1d:

>>> a = np.zeros(0)
>>> for i in range(3):
...     a = np.r_[a, [i, i, i]]
...
>>> a
array([ 0.,  0.,  0.,  1.,  1.,  1.,  2.,  2.,  2.])

Concatenate rows:

>>> a = np.zeros((0,3))
>>> for i in range(3):
...     a = np.r_[a, [[i,i,i]]]
...
>>> a
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.],
       [ 2.,  2.,  2.]])

Concatenate columns:

>>> a = np.zeros((3,0))
>>> for i in range(3):
...     a = np.c_[a, [[i],[i],[i]]]
...
>>> a
array([[ 0.,  1.,  2.],
       [ 0.,  1.,  2.],
       [ 0.,  1.,  2.]])
2 of 6
37

numpy.append is pretty different from list.append in python. I know that's thrown off a few programers new to numpy. numpy.append is more like concatenate, it makes a new array and fills it with the values from the old array and the new value(s) to be appended. For example:

import numpy

old = numpy.array([1, 2, 3, 4])
new = numpy.append(old, 5)
print old
# [1, 2, 3, 4]
print new
# [1, 2, 3, 4, 5]
new = numpy.append(new, [6, 7])
print new
# [1, 2, 3, 4, 5, 6, 7]

I think you might be able to achieve your goal by doing something like:

result = numpy.zeros((10,))
result[0:2] = [1, 2]

# Or
result = numpy.zeros((10, 2))
result[0, :] = [1, 2]

Update:

If you need to create a numpy array using loop, and you don't know ahead of time what the final size of the array will be, you can do something like:

import numpy as np

a = np.array([0., 1.])
b = np.array([2., 3.])

temp = []
while True:
    rnd = random.randint(0, 100)
    if rnd > 50:
        temp.append(a)
    else:
        temp.append(b)
    if rnd == 0:
         break

 result = np.array(temp)

In my example result will be an (N, 2) array, where N is the number of times the loop ran, but obviously you can adjust it to your needs.

new update

The error you're seeing has nothing to do with types, it has to do with the shape of the numpy arrays you're trying to concatenate. If you do np.append(a, b) the shapes of a and b need to match. If you append an (2, n) and (n,) you'll get a (3, n) array. Your code is trying to append a (1, 0) to a (2,). Those shapes don't match so you get an error.

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.append.html
numpy.append — NumPy v2.5 Manual
>>> a = np.array([1, 2], dtype=np.int_) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64 · Default dtype for empty ndarrays is float64 thus making the output of dtype float64 when appended with dtype int64 · Go BackOpen In Tab ·
🌐
Python Guides
pythonguides.com › python-numpy-empty-array
Create An Empty Array Using NumPy In Python
May 7, 2025 - NumPy arrays have a fixed size. np.append cannot extend one, so it allocates a brand new array and copies everything across, every single iteration. Build a plain Python list and convert once at the end. Lists over-allocate, so appending to them is cheap. If you know the final length in advance, allocate with np.empty(n) up front and assign by index.
🌐
Appdividend
appdividend.com › appending-a-new-row-to-an-empty-array-in-numpy
How to Append a New Row to an Empty Array in Numpy
September 7, 2025 - The easiest and most straightforward way to append a new row to an empty numpy array is to use the “numpy.append()” method.
🌐
IncludeHelp
includehelp.com › python › how-to-add-a-new-row-to-an-empty-numpy-array.aspx
How to add a new row to an empty NumPy array?
May 26, 2023 - The numpy.append() appends values to the end of an array and it can also be used to add a new row in an empty NumPy array.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.append.html
numpy.append — NumPy v2.3 Manual
>>> a = np.array([1, 2], dtype=int) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64 · Default dtype for empty ndarrays is float64 thus making the output of dtype float64 when appended with dtype int64 · Go BackOpen In Tab ·
🌐
Tudelft
nsweb.tn.tudelft.nl › ~gsteele › TN2513_Tips › Creating a numpy array in a loop.html
Creating a numpy array in a loop
Numpy also has an append function, but it does not append to a given array, it instead creates a new array with the results appended. This is both memory inefficient, and also computationally inefficient. You should only use np.append if you really want a second copy of the array.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.append.html
numpy.append — NumPy v2.2 Manual
>>> a = np.array([1, 2], dtype=int) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64 · Default dtype for empty ndarrays is float64 thus making the output of dtype float64 when appended with dtype int64 ·
🌐
Medium
medium.com › @whyamit101 › how-to-append-to-a-ndarray-4d73c600f5e5
How to Append to a ndarray. If you think you need to spend $2,000… | by why amit | Medium
February 9, 2025 - It can be a single value, a list, or even another array. axis (optional): This determines whether you append along a specific axis or flatten the data into one dimension. If you set axis=None (default), the function flattens the result. If you specify an axis (e.g., axis=0), the values will be appended along that dimension. Let’s See It in Action! ... import numpy as np # Original array arr = np.array([10, 20, 30]) # Appending a single value new_arr = np.append(arr, 40) print("After appending 40:", new_arr) # Appending multiple values new_arr = np.append(arr, [50, 60]) print("After appending [50, 60]:", new_arr)
🌐
IncludeHelp
includehelp.com › python › concatenating-empty-array-in-numpy.aspx
Python - Concatenating empty array in NumPy
We will use the vstack() method to concatenate the empty array in the original numpy array.