append() creates a new array which can be the old array with the appended element.

I think it's more normal to use the proper method for adding an element:

a = numpy.append(a, a[0])
Answer from steabert on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.append.html
numpy.append — NumPy v2.5 Manual
>>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Discussions

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 - How to append a NumPy array to a NumPy array - Stack Overflow
I'm trying to populate a NumPy array of NumPy arrays. Every time I finish an iteration of a loop I create the array to be added. I would then like to append that array to the end of another array. ... More on stackoverflow.com
🌐 stackoverflow.com
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
python - How to append arrays to another numpy array? - Stack Overflow
I am trying to loop through a set of coordinates and 'stacking' these arrays of coordinates to another array (so in essence I want to have an array of arrays) using numpy. This is my attempt: import More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.append.html
numpy.append — NumPy v2.3 Manual
>>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-append-python
numpy.append() in Python - GeeksforGeeks
April 14, 2025 - import numpy as geek arr1 = geek.arange(8).reshape(2, 4) print("2D arr1 :", arr1) print("Shape :", arr1.shape) arr2 = geek.arange(8, 16).reshape(2, 4) print("2D arr2:", arr2) print("Shape :", arr2.shape) arr3 = geek.append(arr1, arr2) print("Appended arr3 by flattened :", arr3) arr3 = geek.append(arr1, arr2, axis = 0) print("Appended arr3 with axis 0 :", arr3) arr3 = geek.append(arr1, arr2, axis = 1) print("Appended arr3 with axis 1 :", arr3) ... When no axis is given both arrays are flattened into a single long 1D array and then joined. When axis=0 rows from the second array are added below the first. With axis=1 columns are added to the right side of the first array. numpy.append() function is used to extend a array and it creates a new array rather than modifying the original one.
🌐
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-array
Python Array Add: How to Append, Extend & Insert Elements | DigitalOcean
June 11, 2026 - To add to an array in Python, you append, extend, or insert elements depending on the structure you use. Most tutorials mean a Python list when they say “array.” For typed numeric storage, use the stdlib array module. For math on multi-dimensional data, use NumPy.
Find elsewhere
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.append.html
numpy.append — NumPy v2.6.dev0 Manual
>>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: append() to add values to an array | note.nkmk.me
February 4, 2024 - NumPy: Join arrays with np.concatenate, block, vstack, hstack, etc. The approach for three-dimensional and higher-dimensional arrays follows the same principle. By default (axis=None), the array is flattened to one dimension before being added to the end. a_3d = np.arange(12).reshape(2, 3, 2) print(a_3d) # [[[ 0 1] # [ 2 3] # [ 4 5]] # # [[ 6 7] # [ 8 9] # [10 11]]] print(np.append(a_3d, 100)) # [ 0 1 2 3 4 5 6 7 8 9 10 11 100]
🌐
Tudelft
nsweb.tn.tudelft.nl › ~gsteele › TN2513_Tips › Creating a numpy array in a loop.html
Creating a numpy array in a loop
One way to do this is to use a list to collect your values and then convert it at the end to a numpy array. If you like lists, this is quick and handy. ... # Make an empty list y = [] # Grow it by appending values for x in range(10): temp = x**2 # this would usually be a more complicated ...
🌐
Programiz
programiz.com › python-programming › numpy › methods › append
NumPy append()
The append() method adds the values at the end of a NumPy array. Example import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # append array2 to array1 array3 = np.append(array1, array2) print(array3) # Output : [1 2 3 4 5 6] append() Syntax The syntax of append() is:
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › append
Python Numpy append() - Add Elements to Array | Vultr Docs
April 10, 2025 - Here, python append to numpy array is done efficiently by appending elements to a list first, then converting it to a NumPy array in one step.
🌐
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
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.

🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.concatenate.html
numpy.concatenate — NumPy v2.5 Manual
When one or more of the arrays to be concatenated is a MaskedArray, this function will return a MaskedArray object instead of an ndarray, but the input masks are not preserved.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_append.htm
Numpy Append() Method
The Numpy Append() method adds values to the end of an input array, allocating a new array for the result rather than modifying the original in place. If no axis is specified then both the array and values are flattened before appending.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to append numpy arrays examples
How to Append NumPy Arrays Examples - Spark By {Examples}
March 27, 2024 - numpy.append() is used to append two or multiple arrays at the end of the specified NumPy array. The NumPy append() function is a built-in function