if you know the number of columns before hand:

>>> xs = np.array([[1,2,3,4,5],[10,20,30,40,50]])
>>> ys = np.array([], dtype=np.int64).reshape(0,5)
>>> ys
array([], shape=(0, 5), dtype=int64)
>>> np.vstack([ys, xs])
array([[  1.,   2.,   3.,   4.,   5.],
       [ 10.,  20.,  30.,  40.,  50.]])

if not:

>>> ys = np.array([])
>>> ys = np.vstack([ys, xs]) if ys.size else xs
array([[ 1,  2,  3,  4,  5],
       [10, 20, 30, 40, 50]])
Answer from behzad.nouri on Stack Overflow
🌐
GitHub
github.com › numpy › numpy › issues › 12453
stack with an empty matrix · Issue #12453 · numpy/numpy · GitHub
November 26, 2018 - Redefine stack functions, when stacked with an empty matrix, it equals to itself. import numpy as np np.vstack(([], A)) == np.hstack(([], A)) == A (== np.concatenate(([], A))) # pseudo code A = array([]) for-loop A1 = ... if A is empty A = A1 ...
🌐
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. The numpy.vstack() method is used to vertically stack an array to the existing array.
🌐
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 - This makes it an excellent choice for appending new rows to an empty NumPy array. The np.vstack() function has a straightforward syntax: ... tup: This parameter expects a tuple of arrays that you want to stack vertically.
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!

Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.vstack.html
numpy.vstack — NumPy v2.5 Manual
This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions concatenate, stack and block provide more general stacking and concatenation operations.
🌐
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.
🌐
GitHub
github.com › numpy › numpy › issues › 12628
vstack-ing empty list · Issue #12628 · numpy/numpy · GitHub
December 28, 2018 - It is clear that the empty list does not allow numpy to infer on the shape and check if it is compatible with the other arrays being stacked. However, as stacking the empty list doesn't change anything, I think some branching in vstack (or the prefiltering of empty arrays in vstack) could make this code work.
Author: numpy
🌐
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 › doc › stable › reference › generated › numpy.dstack.html
numpy.dstack — NumPy v2.5 Manual
This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r/g/b channels (third axis). The functions concatenate, stack and block provide more general stacking and concatenation operations.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python numpy howtos" › "how to create empty numpy array"
How to Create Empty NumPy Array | Delft Stack
March 11, 2025 - The numpy.zeros() function is a straightforward way to create an empty array filled with zeros. This method is particularly useful when you need to initialize an array of a specific shape and size, with all elements set to zero.
🌐
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.
Top answer
1 of 2
1

If by empty array, you mean an array with 0 dimensions, you can simply convert to a NumPy array an empty list:

import numpy as np

np.array([])
# array([], dtype=float64)

or use any of the NumPy's initialization functions with a (0,) or 0 shape, e.g.:

np.zeros((0,))
# array([], dtype=float64)

np.ones((0,))
# array([], dtype=float64)

np.empty((0,))
# array([], dtype=float64)

np.full((0,), 0.0)                                                                          
# array([], dtype=float64)

etc.


If by empty you mean an array of given size but not initialized (i.e. you only ask the OS for the required memory), you can use np.empty() specifying the size you need, e.g.:

np.empty((2, 3))
# array([[1.46643506e-316, 0.00000000e+000, 0.00000000e+000],
#        [0.00000000e+000, 0.00000000e+000, 0.00000000e+000]])
2 of 2
0

There are two ways an empty NumPy array can be created: numpy.zeros and numpy.empty.

The syntax for using numpy.zeros and numpy.empty is shown below:

numpy.zeros(shape, dtype=float, order='C')
numpy.empty(shape, dtype=float, order='C')

# Shape -> Shape of the new array, e.g., (2, 3) or 2.

# dtype -> The desired data-type for the array,e.g., numpy.int8. Default is numpy.float64. This parameter is optional.

# order -> Indicates whether multi-dimensional data should be stored in row-major (C-style) or column-major (Fortran-style) order in memory. This parameter is optional.

how to use numpy.zeros to create an empty NumPy array:

import numpy as np
myArr = np.zeros((2,3))
print(myArr)

how to use numpy.empty to create an empty NumPy array:

import numpy as np
myArr = np.empty((2,3))
print(myArr)

numpy.empty, unlike numpy.zeros, does not set the array values to zero and, ​therefore, may be marginally faster. On the other hand, it requires the user to manually set all the values in the array ​and should be used with caution.

Top answer
1 of 3
2

Like Oniow said, concatenate does exactly what you saw.

If you want 'default values' that will differ from regular scalar elements, I would suggest you to initialize your array with NaNs (as your 'default value'). If I understand your question, you want to merge matrices so that regular scalars will override your 'default value' elements.

Anyway I suggest you to add the following:

def get_default(size_x,size_y):
    # returns a new matrix filled with 'default values'
    tmp = np.empty([size_x,size_y])
    tmp.fill(np.nan)
    return tmp

And also:

def merge(a, b):
    l = lambda x, y: y if np.isnan(x) else x
    l = np.vectorize(l)
    return map(l, a, b)

Note that if you merge 2 matrices, and both values are non 'default' then it will take the value of the left matrix.

Using NaNs as default value, will result the expected behavior from a default value, for example all math ops will result 'default' as this value indicates that you don't really care about this index in the matrix.

2 of 3
1

If I understand your question correctly - concatenate is not what you are looking for. Concatenate does as you saw: joins along an axis.

If you are trying to have an empty matrix that becomes the values of another you could do the following:

import numpy as np

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

my_array = a + b

--or--

import numpy as np

my_array = np.zeros([2,2]) # you can use empty here instead in this case.

my_array[0,0] = float(input('Enter first value: ')) # However you get your data to put them into the arrays.

But, I am guessing that is not what you really want as you could just use my_array = b. If you edit your question with more info I may be able to help more.

If you are worried about values adding over time to your array...

import numpy as np

a = np.zeros([2,2])

my_array = b # b is some other 2x2 matrix
''' Do stuff '''
new_b # New array has appeared
my_array = new_b # update your array to these new values. values will not add. 

# Note: if you make changes to my_array those changes will carry onto new_b. To avoid this at the cost of some memory:
my_array = np.copy(new_b)