Appending to numpy arrays is very inefficient. This is because the interpreter needs to find and assign memory for the entire array at every single step. Depending on the application, there are much better strategies.

If you know the length in advance, it is best to pre-allocate the array using a function like np.ones, np.zeros, or np.empty.

desired_length = 500
results = np.empty(desired_length)
for i in range(desired_length):
    results[i] = i**2

If you don't know the length, it's probably more efficient to keep your results in a regular list and convert it to an array afterwards.

results = []
while condition:
    a = do_stuff()
    results.append(a)
results = np.array(results)

Here are some timings on my computer.

def pre_allocate():
    results = np.empty(5000)
    for i in range(5000):
        results[i] = i**2
    return results

def list_append():
    results = []
    for i in range(5000):
        results.append(i**2)
    return np.array(results)

def numpy_append():
    results = np.array([])
    for i in range(5000):
        np.append(results, i**2)
    return results

%timeit pre_allocate()
# 100 loops, best of 3: 2.42 ms per loop

%timeit list_append()
# 100 loops, best of 3: 2.5 ms per loop

%timeit numpy_append()
# 10 loops, best of 3: 48.4 ms per loop

So you can see that both pre-allocating and using a list then converting are much faster.

Answer from Roger Fan on Stack Overflow
🌐
DataCamp
datacamp.com › doc › numpy › append
NumPy append()
NumPy's `append()` function is used to add elements to the end of an existing array, effectively creating a new array with the additional elements.
Discussions

python - Optimal way to append to numpy array - Stack Overflow
I have a numpy array and I can simply append an item to it using append, like this: numpy.append(myarray, 1) In this case I just appended the integer 1. But is this the quickest way to append to an More on stackoverflow.com
🌐 stackoverflow.com
Issues with Optimization / Appending to a Numpy Array
Numpy arrays are poorly suited to appending because they’re fixed-length, and doing an append operation copies the entire array to a new array with one more spot, and then puts the new thing in that spot. If your main operation is appending then you’re likely better off with standard Python lists, or alternatively you can implement your own appending algorithm that does larger resizes (e.g. double each time) and separately keep track of how many elements have been put into each territory so that you only need to resize when you run out of space again. More generally, it’s not clear what you’re actually doing here. It sounds like you’re using a sparse array type approach where you only store individual position coordinates, but if you’re in charge of managing the whole map maybe you’re better off just having a single map array that keeps track of which territory ‘owns’ each pixel? Note that numpy is optimised for vectorised operations that apply to a large portion of an array at the same time. If you find yourself looping through a numpy array there’s probably something that could be handled better with vectorised operations, and if not it’s possible a numpy array is the wrong data structure to use :-) More on reddit.com
🌐 r/learnpython
11
2
October 31, 2021
python - Add single element to array in numpy - Stack Overflow
NOTE: All answers below copy the array, which means if you append in a loop you get O(n^2) behavior. There is .resize() which might be O(n). See How to extend an array in-place in Numpy? More on stackoverflow.com
🌐 stackoverflow.com
python - How to use the function numpy.append - Stack Overflow
I have a problem using the function numpy.append. I wrote the following function as part of a larger piece of code, however, my error is reproduced in the folowing: data = [ [ '... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-append-python
numpy.append() in Python - GeeksforGeeks
April 14, 2025 - numpy.append() function is used to add new values at end of existing NumPy array. This is useful when we have to add more elements or rows in existing numpy array.
Top answer
1 of 2
33

Appending to numpy arrays is very inefficient. This is because the interpreter needs to find and assign memory for the entire array at every single step. Depending on the application, there are much better strategies.

If you know the length in advance, it is best to pre-allocate the array using a function like np.ones, np.zeros, or np.empty.

desired_length = 500
results = np.empty(desired_length)
for i in range(desired_length):
    results[i] = i**2

If you don't know the length, it's probably more efficient to keep your results in a regular list and convert it to an array afterwards.

results = []
while condition:
    a = do_stuff()
    results.append(a)
results = np.array(results)

Here are some timings on my computer.

def pre_allocate():
    results = np.empty(5000)
    for i in range(5000):
        results[i] = i**2
    return results

def list_append():
    results = []
    for i in range(5000):
        results.append(i**2)
    return np.array(results)

def numpy_append():
    results = np.array([])
    for i in range(5000):
        np.append(results, i**2)
    return results

%timeit pre_allocate()
# 100 loops, best of 3: 2.42 ms per loop

%timeit list_append()
# 100 loops, best of 3: 2.5 ms per loop

%timeit numpy_append()
# 10 loops, best of 3: 48.4 ms per loop

So you can see that both pre-allocating and using a list then converting are much faster.

2 of 2
2

If you know the size of the array at the end of the run, then it is going to be much faster to pre-allocate an array of the appropriate size and then set the values. If you do need to append on-the fly, it's probably better to try to not do this one element at a time, instead appending as few times as possible to avoid generating many copies over and over again. You might also want to do some profiling of the difference in timings of np.append, np.hstack, np.concatenate. etc.

Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › issues with optimization / appending to a numpy array
r/learnpython on Reddit: Issues with Optimization / Appending to a Numpy Array
October 31, 2021 -

https://paste.ofcode.org/zpT6HK22LuswbXhTHZfUK6

I am working on a starter project which involves storing pixel coordinates for various "territories" (dictionaries) on a map. Because there are a huge amount of coordinates (33 million pixels), I have been trying to optimize the program so it uses less memory. I have been using numpy's int16 type and ndarray to save memory. The problem I am getting however, is with adding pixels to territories. Each territory (dictionary) has a "pixels" key with an array as a value, and to add pixels to that array I use the following method:

def addPixel(territory, pixel):
    pixels = getProperty(territory, "pixels")
    territory["pixels"] = numpy.append(pixels, pixel)

This method, however, appears to be very slow, and it would take a huge amount of time to iterate over millions of pixels. Does anyone know of a faster way the append items to ndarrays?

Top answer
1 of 2
2
Numpy arrays are poorly suited to appending because they’re fixed-length, and doing an append operation copies the entire array to a new array with one more spot, and then puts the new thing in that spot. If your main operation is appending then you’re likely better off with standard Python lists, or alternatively you can implement your own appending algorithm that does larger resizes (e.g. double each time) and separately keep track of how many elements have been put into each territory so that you only need to resize when you run out of space again. More generally, it’s not clear what you’re actually doing here. It sounds like you’re using a sparse array type approach where you only store individual position coordinates, but if you’re in charge of managing the whole map maybe you’re better off just having a single map array that keeps track of which territory ‘owns’ each pixel? Note that numpy is optimised for vectorised operations that apply to a large portion of an array at the same time. If you find yourself looping through a numpy array there’s probably something that could be handled better with vectorised operations, and if not it’s possible a numpy array is the wrong data structure to use :-)
2 of 2
2
If you can pre-estimate the final size of the array, allocate it with that size using numpy.empty and then fill it without using append. If you can't predict the final size, append to plain old lists and when you have all the data in one list, then create the numpy array. When you append to a numpy array you end up copying all of the data into a new array so it is expensive for large arrays.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ma.append.html
numpy.ma.append — NumPy v2.4 Manual
A copy of a with b appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, the result is a flattened array. ... Equivalent function in the top-level NumPy module.
🌐
Medium
medium.com › @whyamit101 › how-to-append-two-arrays-in-numpy-efa74e5e2788
How to Append Two Arrays in NumPy? | by why amit | Medium
February 26, 2025 - We’re passing a list of arrays ([arr1, arr2, arr3]) to np.append(). NumPy flattens them into a single array because we didn’t specify an axis.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.concatenate.html
numpy.concatenate — NumPy v2.5 Manual
>>> import numpy as np >>> a = np.array([[1, 2], [3, 4]]) >>> b = np.array([[5, 6]]) >>> np.concatenate((a, b), axis=0) array([[1, 2], [3, 4], [5, 6]]) >>> np.concatenate((a, b.T), axis=1) array([[1, 2, 5], [3, 4, 6]]) >>> np.concatenate((a, b), axis=None) array([1, 2, 3, 4, 5, 6])
🌐
Programiz
programiz.com › python-programming › numpy › methods › append
NumPy append()
The append() method returns a copy of the array with values appended. import numpy as np array1 = np.array([0, 1, 2, 3]) array2 = np.array([4, 5, 6, 7])
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › append
Python Numpy append() - Add Elements to Array | Vultr Docs
April 10, 2025 - Plan to append elements to this array. ... Use numpy.append() to add a single element or multiple elements.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_append_values_to_an_array.htm
NumPy - Append Values to an Array
Since NumPy arrays have a fixed size, this operation creates a new array with the original elements plus the newly appended values. To achieve this, we can use the np.append() function in NumPy.
🌐
Medium
medium.com › @amit25173 › numpy-append-in-python-60071c826fc4
numpy.append() in Python:. I understand that learning data science… | by Amit Yadav | Medium
January 22, 2025 - If you specify an axis, the function appends elements along that axis. But beware — shapes need to match! ... Imagine you’re working with an array of data, and suddenly, a new value or row comes up. Instead of creating a new array manually, numpy.append() does all the heavy lifting for you.
🌐
w3resource
w3resource.com.cach3.com › numpy › manipulation › append.php.html
Numpy: append() function - w3resource
>>> import numpy as np >>> np.append([[0, 1, 2], [3, 4, 5]],[[6, 7, 8]], axis=0) array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: append() to add values to an array | note.nkmk.me
February 4, 2024 - In NumPy, the np.append() function allows you to add values (elements, rows, or columns) to either the end or the beginning of an array (ndarray). numpy.append — NumPy v1.26 Manual Note that append() ...