Don't concatenate/append/stack arrays if you can help it, especially big ones. It's very wasteful of memory and slow.

Assign A = np.empty((m, n+2, n+2)) and then fill it with A[i] = np.r_[S1, np.c_[S2, Sc[i], S2], S1]. Or do it vectorized and get rid of the for loops:

A = np.zeros((m, n+2, n+2))
A[:,1:-1,1:-1] = Sc

or even do it in one line:

A = np.pad(Sc, ((0,0),(1,1),(1,1)), 'constant', constant_values = 0)
Answer from Daniel F on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: append() to add values to an array | note.nkmk.me
February 4, 2024 - 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 ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.append.html
numpy.append โ€” NumPy v2.5 Manual
Delete elements from an array. ... Try it in your browser! >>> import numpy as np >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) array([1, 2, 3, ..., 7, 8, 9])
Discussions

How to append 3d numpy array to a 4d array

Sounds like what you really need is a python list of 3D numpy arrays. Appending to a numpy array is possible with np.append or np.concat, but it's very expensive because it forces the entire array to be remade. Is there any reason you want a 4D array?

More on reddit.com
๐ŸŒ r/learnpython
8
1
December 29, 2020
python - Append 2D array to 3D array, extending third dimension - Stack Overflow
I have an array A that has shape (480, 640, 3), and an array B with shape (480, 640). How can I append these two as one array with shape (480, 640, 4)? I tried np.append(A,B) but it doesn't keep... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Creating a 3d numpy array matrix using append method - Stack Overflow
Is there a way to create a 3d numpy array by appending 2d numpy arrays? What I currently do is append my 2d numpy array into an initialized list of pre determined 2d numpy array, i.e., List=[np.zer... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - numpy: Append row to a individual 3D array - Stack Overflow
There's numerous posts and blogs talking about how to manipulate 2D arrays using append, vstack or concatenate, but I couldn't make it work in 3D. Problem Assumptions: --The 3D array will have the More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ append
NumPy append()
import numpy as np arr = np.array([1, 2, 3]) new_arr = np.append(arr, [4, 5]) In this example, the elements [4, 5] are appended to the array arr, resulting in new_arr being [1, 2, 3, 4, 5].
Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 28246025 โ€บ numpy-append-row-to-a-individual-3d-array โ€บ 28246185
python - numpy: Append row to a individual 3D array - Stack Overflow
There's numerous posts and blogs talking about how to manipulate 2D arrays using append, vstack or concatenate, but I couldn't make it work in 3D. ... CopyIn [1]: import numpy as np In [2]: a = np.empty((3, 1, 2)) Out[2]: array([[[0., 0.]], [[0., 0.]], [[0., 0.]]]) In [3]: a[0] = [[5, 6]] In [4]: a Out[4]: array([[[5., 6.]], [[0., 0.]], [[0., 0.]]]) In [5]: a[0] = np.vstack((a[0], [[10, 15]])) Out[5]: ValueError: could not broadcast input array from shape (2,2) into shape(1,2) In [6]: a[0] = np.append(a[0], [[10, 15]], axis=0) Out[6]: ValueError: could not broadcast input array from shape (2,2) into shape(1,2)
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.4 โ€บ reference โ€บ generated โ€บ numpy.append.html
numpy.append โ€” NumPy v2.4 Manual
Delete elements from an array. ... Try it in your browser! >>> import numpy as np >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) array([1, 2, 3, ..., 7, 8, 9])
Top answer
1 of 1
2

You can allocate extra space for your array grid_data, fill it with the NaN, and keep track of the next index to be filled in another array while iterating through and filling with values from data. If you completely fill the third dimension for some lat_idx, lon_idx with non-NaN values, then you just allocate more space. Since appending is expensive with numpy, it's best that this extra space is pretty large so you only do it once or twice (below I allocate twice the original space).

Once the array is filled, you can remove the added space that was unused with numpy.isnan(). This solution does what you want but is very slow (for the example values you gave it took about two minutes), but the slow execution comes from iterating rather than the numpy operations.

Here's the code:

import random
import numpy as np

grid_data = np.ones(shape=(121, 201, 1000))
data = np.random.rand(4800, 4800)

# keep track of next index to fill for all the arrays in axis 2
next_to_fill = np.full(shape=(grid_data.shape[0], grid_data.shape[1]),
                       fill_value=grid_data.shape[2],
                       dtype=np.int32)

# allocate more space
double_shape = (grid_data.shape[0], grid_data.shape[1], grid_data.shape[2] * 2)
extra_space = np.full(shape=double_shape, fill_value=np.nan)
grid_data = np.append(grid_data, extra_space, axis=2)

for row in range(4800):
    for col in range(4800):
        lat_idx = random.randint(0, 120)
        lon_idx = random.randint(0, 200)

        # allocate more space if needed
        if next_to_fill[lat_idx, lon_idx] >= grid_data.shape[2]:
            grid_data = np.append(grid_data, extra_space, axis=2)

        grid_data[lat_idx, lon_idx, next_to_fill[lat_idx, lon_idx]] = data[row,
                                                                           col]
        next_to_fill[lat_idx, lon_idx] += 1

# remove unnecessary nans that were appended
not_all_nan_idxs = ~np.isnan(grid_data).all(axis=(0, 1))
grid_data = grid_data[:, :, not_all_nan_idxs]
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_append.htm
Numpy Append() Method
import numpy as np a = ... [[5,5,5],[7,8,9]],axis = 1)) First array: [[1 2 3] [4 5 6]] Append elements to array: [1 2 3 4 5 6 7 8 9] Append elements along axis 0: [[1 2 3] [4 5 6] [7 8 9]] Append elements along axis 1: ...
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.append.html
numpy.append โ€” NumPy v2.6.dev0 Manual
Delete elements from an array. ... Try it in your browser! >>> import numpy as np >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) array([1, 2, 3, ..., 7, 8, 9])
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ numpy-append-in-python
numpy.append() in Python | DigitalOcean
Technical tutorials, Q&A, events โ€” This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.append.html
numpy.append โ€” NumPy v2.2 Manual
Delete elements from an array. ... >>> import numpy as np >>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]) array([1, 2, 3, ..., 7, 8, 9])
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_append_values_to_an_array.htm
NumPy - Append Values to an Array
Appending to multi-dimensional arrays in NumPy involves adding new elements along specified axes. Unlike 1D and 2D arrays, multi-dimensional arrays (e.g., 3D or higher) require careful alignment of the dimensions and axes along which you want to append data. In the following example, we are using the np.append() function to add values to a 3D array along the first axis โˆ’
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-numpy-concatenate
Python numpy concatenate
September 24, 2019 - numpy.concatenate((array1, array2,....), axis = 0) array1, array2,โ€ฆ are the arrays that you want to combine. The arrays that you pass to this function must have the same shape. However, you can choose arrays with different dimensions. axis ...
๐ŸŒ
w3resource
w3resource.com โ€บ numpy โ€บ manipulation โ€บ append.php
Numpy: numpy.append() function - w3resource
April 25, 2026 - Generate a sequence of numbers with a specific pattern by using numpy.append() in a loop. ... append: A copy of arr with values appended along the specified axis. Note that the operation does not modify arr; instead, it allocates a new array and fills it with the appended values. Example: Appending arrays in NumPy using numpy.append()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-append-python
numpy.append() in Python - GeeksforGeeks
April 14, 2025 - With the help of Numpy numpy.ndarray.__add__(), we can add a particular value that is provided as a parameter in the ndarray.__add__() method. Value will be added to each and every element in a numpy array. Syntax: ndarray.__add__($self, value, /) Return: self+value Example #1 : In this example we c
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ generated โ€บ numpy.append.html
numpy.append โ€” NumPy v2.0 Manual
Delete elements from an array. ... When axis is specified, values must have the correct shape. >>> np.append([[1, 2, 3], [4, 5, 6]], [[7, 8, 9]], axis=0) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0) Traceback (most recent call last): ...