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.
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
Creating empty nxn square matrix of 0
It's a list comprehension that creates n distinct [0] * n lists, and it's a pretty common idiom. You can think of [[0] * n] * n as a = [0] * n b = [] for _ in range(n): b.append(a) # same `a` every time And the later as b = [] for _ in range (n): a = [0] * n b.append(a) # new `a` every time The i is irrelevant, and you'd commonly just use a name like _ communicating "this variable doesn't matter". More on reddit.com
๐ŸŒ r/learnpython
1
1
July 13, 2023
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 5, 2022
Why are 2D arrays in Python so stupid?
Because they aren't arrays, and aren't designed to be pre-allocated like that. Generally, if you need matrices use Numpy. More on reddit.com
๐ŸŒ r/learnprogramming
4
0
September 5, 2019
๐ŸŒ
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 ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ some
Array.prototype.some() - JavaScript | MDN
1 week ago - Otherwise, if callbackFn returns ... some() acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it returns false for any condition....
๐ŸŒ
Replit
replit.com โ€บ home โ€บ discover โ€บ how to create an empty array in python
How to create an empty array in Python | Replit
2 weeks ago - The function np.empty() creates an array of a given size without initializing its entries to any particular value. The output shows arbitrary numbers because the function simply allocates a block of memory and returns whatever "garbage" values ...
๐ŸŒ
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 - 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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 with the same shape and data type (dtype) as an existing array, use np.empty_like().
๐ŸŒ
Quora
quora.com โ€บ How-do-I-check-if-an-array-is-empty-in-Python
How to check if an array is empty in Python - Quora
Answer (1 of 12): I am going to assume you are talking about lists (Python does have arrays, but they are very different to lists). Three ways : 1 Test the truthiness If you know the item is a list you do : [code]if not my_list: print(โ€˜List is emptyโ€™) [/code]Empty containers (lists,sets,t...
๐ŸŒ
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).

๐ŸŒ
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). To make an empty list, you ...
๐ŸŒ
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.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ itertools.html
itertools โ€” Functions creating iterators for efficient looping
1 week ago - The number of 2-tuples in the output iterator will be one fewer than the number of inputs. It will be empty if the input iterable has fewer than two values.
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ array-creation โ€บ empty.php
NumPy: numpy.empty() function - w3resource
June 8, 2024 - NumPy array creation: numpy.empty() function, example - Return a new array of given shape and type, without initializing entries.
๐ŸŒ
Appdividend
appdividend.com โ€บ create-and-check-if-a-numpy-array-is-empty
How to Create and Check If a Numpy Array is Empty
July 10, 2025 - The output is a one-dimensional array with no values. Its shape is (0,). This approach is direct and readable. For creating a 2D or multidimensional empty array, the better approach is to use the โ€œnp.empty()โ€ function.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-check-if-list-empty-not
Check if a list is empty or not in Python - GeeksforGeeks
October 24, 2024 - Explanation: len(a) returns 0 for an empty list. Lists can be directly evaluated in conditions. Python considers an empty list as False.
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ declare an empty array in python
Declare an empty array in Python - CodeSpeedy
September 19, 2023 - You can check out our tutorial on Array creation in Numpy . I have used the empty() function. It takes a number as input which defines the length of the empty array. The other attribute is dtype, which I have defined as an object.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-empty-list
A Comprehensive Guide to Python Empty Lists | DataCamp
February 2, 2024 - def find_strings_starting_with... 'Function', 'Array', 'Arguments']) # 'result' will contain ['Assert', 'Array', 'Arguments'] In summary, mastering empty lists and their operations is crucial in Python programming, allowing for versatile data manipulation....
๐ŸŒ
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.