How To Append To Numpy Array

9 min read

Appending data to a NumPy array is a fundamental operation in scientific computing with Python, yet it behaves differently than appending to a standard Python list. On top of that, because NumPy arrays are stored in contiguous blocks of memory with a fixed size and homogeneous data type, they cannot be resized in place. Every operation that appears to "append" data actually creates an entirely new array, copies the existing data over, and adds the new elements. Understanding this mechanism is critical for writing performant code, especially when working with large datasets where repeated copying can become a significant bottleneck Nothing fancy..

Understanding the Core Mechanism

Before diving into syntax, it is essential to grasp why NumPy handles appending this way. A NumPy ndarray, however, is a single contiguous block of memory containing raw C-style values (integers, floats, etc.Still, ). Think about it: a Python list is an array of pointers to objects scattered in memory; adding an item simply appends a new pointer. This design allows for vectorized operations and cache efficiency but makes dynamic resizing impossible without memory reallocation.

When you call an append function, NumPy must:

  1. In practice, allocate a new block of memory large enough for the old data plus the new data. 2. Copy the old data into the new block. And 3. Copy the new data into the remaining space.
  2. Deallocate the old block (handled by Python's garbage collector).

This $O(N)$ copy operation happens every single time you append. Doing this inside a loop results in $O(N^2)$ time complexity, which is the most common performance pitfall for beginners Not complicated — just consistent. Less friction, more output..

The Primary Tool: numpy.append()

The most direct way to add elements is the numpy.append() function. Despite its name, it does not modify the original array; it returns a new one.

Syntax and Parameters

numpy.append(arr, values, axis=None)
  • arr: The input array. Values are appended to a copy of this array.
  • values: The values to append. Must be broadcastable to the shape of arr (excluding the axis of appending).
  • axis: The axis along which values are appended. If None (default), both arrays are flattened before concatenation.

Appending to 1-D Arrays (Flattened Behavior)

For one-dimensional arrays, the behavior is intuitive and similar to list extension That's the part that actually makes a difference..

import numpy as np

arr = np.array([1, 2, 3])
new_arr = np.append(arr, [4, 5])

print(arr)      # Output: [1 2 3]  (Original unchanged)
print(new_arr)  # Output: [1 2 3 4 5]

Note that passing a scalar works, but passing a list or another array is standard practice. If you pass a scalar, it is treated as a single element.

Appending to Multi-Dimensional Arrays: The axis Parameter

This is where confusion often arises. With multi-dimensional arrays, the axis parameter dictates the direction of growth.

Appending Rows (axis=0): To add a new row to a 2D matrix, the new data must have the same number of columns.

matrix = np.array([[1, 2], [3, 4]]) # Shape (2, 2)
new_row = np.array([[5, 6]])        # Shape (1, 2) - Note the double brackets for 2D

result = np.append(matrix, new_row, axis=0)
print(result)
# [[1 2]
#  [3 4]
#  [5 6]]

Appending Columns (axis=1): To add a column, the new data must have the same number of rows.

matrix = np.array([[1, 2], [3, 4]]) # Shape (2, 2)
new_col = np.array([[5], [6]])      # Shape (2, 1)

result = np.append(matrix, new_col, axis=1)
print(result)
# [[1 2 5]
#  [3 4 6]]

The Flattening Trap (axis=None): If you omit axis on a 2D array, NumPy flattens both arrays into 1D before concatenating. This destroys the matrix structure.

matrix = np.array([[1, 2], [3, 4]])
result = np.append(matrix, [[5, 6]]) # axis=None by default
print(result) # [1 2 3 4 5 6] -> Shape (6,), not (3, 2)

Always specify axis explicitly when working with dimensions $\ge 2$ to avoid silent logic errors Turns out it matters..

Alternative Concatenation Functions

While np.append is convenient for single additions, NumPy provides a family of stacking functions that are often more readable and slightly faster for specific layouts.

numpy.concatenate()

This is the low-level workhorse. np.append is essentially a wrapper around concatenate. It takes a sequence of arrays (tuple or list) as the first argument That alone is useful..

a = np.array([1, 2])
b = np.array([3, 4])
c = np.array([5, 6])

# Join multiple arrays at once
result = np.concatenate((a, b, c))
# [1 2 3 4 5 6]

Use concatenate when joining more than two arrays simultaneously; it avoids the overhead of multiple intermediate array creations that chained append calls would create Simple, but easy to overlook..

numpy.vstack() and numpy.hstack()

These are helpers for "Vertical Stack" (rows) and "Horizontal Stack" (columns). They handle the reshaping of 1D inputs automatically, which often makes them safer than append with axis.

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

# vstack treats 1D arrays as rows (shape 1, N)
print(np.vstack((a, b)))
# [[1 2 3]
#  [4 5 6]]

# hstack concatenates 1D arrays end-to-end
print(np.hstack((a, b)))
# [1 2 3 4 5 6]

numpy.column_stack() and numpy.row_stack()

column_stack is particularly useful for turning 1D arrays into columns of a 2D matrix.

x = np.array([1, 2, 3])
y = np.array([4, 5, 6])

# Creates a (3, 2) matrix
matrix = np.column_stack((x, y))
print(matrix)
# [[1 4]
#  [2 5]
#  [3 6]]

Performance Best Practices: Pre-allocation vs. Lists

The single most important takeaway for performance is: Never append to a NumPy array inside a loop.

The Anti-Pattern (Slow)

# DO NOT DO THIS FOR LARGE N
result = np.array([])
for i in range(10000):
    result = np.append(result, i * 2) 
    # Allocates and copies memory 10,000 times!

The List-Conversion Pattern (Fast)

Python lists are optimized for dynamic appending (amortized $O(1)$). Build your data in a list, then convert once at the end The details matter here..

# FAST: Python list append is cheap
temp_list = []
for i in range(10000):
    temp_list.append(i * 2)

# Single conversion to NumPy array
result = np.array(temp_list)

The Pre-allocation Pattern (Fastest / Memory Efficient)

If you

If you know the final size of the data before you start processing, the most efficient approach is to allocate a NumPy array of that shape and fill it in place. This eliminates the overhead of repeated memory reallocations and copying that np.append incurs inside a loop The details matter here. Still holds up..

import numpy as np

# Desired final shape and data type
n = 10_000
dtype = np.float32

# Create an empty array (uninitialized values) – fastest allocation
result = np.empty((n,), dtype=dtype)

# Fill it using a vectorised operation or a simple loop
for i in range(n):
    result[i] = i * 2.0          # In practice, prefer vectorised forms:
                                 # result[:] = np.arange(n) * 2.0

Why pre‑allocation works

  • No intermediate copies – The memory block is allocated once and never moved.
  • Predictable memory usage – You can size the array exactly to the needed capacity, avoiding the overallocation that np.append may perform internally.
  • Cache‑friendly – Sequential writes to a contiguous block improve CPU cache performance, especially on large datasets.

Choosing np.empty vs. np.zeros

  • np.empty returns an array with whatever garbage values were already present in the allocated memory. It is the fastest way to create a block because NumPy does not initialise the contents. Use it when you plan to write every element yourself (eresidential generation, custom computation, etc.).
  • np.zeros (or np.ones, np.full) initialises the array to a known value. This is useful when the default value carries semantic meaning (e.g., a mask of False values) or when you want to avoid accidental reads of stale data during development.
# Example: mask for missing entries – initialise to False
mask = np.zeros((n, m), dtype=bool)

# Example: pre‑allocated buffer for a computation that will overwrite everything
buffer = np.empty((batch_size, seq_len, hidden_dim), dtype=np.float16)

Combining pre‑allocation with vectorised operations

If the computation can be expressed without an explicit loop, let NumPy’s internal C‑loops do the work. This not only speeds up execution but also reduces Python‑level overhead.

# Generate 10 000 even numbers in one go
result = np.arange(0, 2 * n, 2, dtype=np.float32)

# Or, for a more complex transformation:
values = np.linspace(0, 1, n)
result = np.sin(values * np.pi)   # vectorised sine

When the data‑generation logic is too involved for a single vectorised call, a hybrid approach can still be efficient: fill the pre‑allocated array using NumPy’s advanced indexing or by broadcasting smaller intermediate arrays.

# Suppose we need result[i] = i * sin(i)
i = np.arange(n, dtype=np.float32)
result = i * np.sin(i)   # No Python loop, fully vectorised

When pre‑allocation isn’t the answer

  • Unknown final size – If you cannot predict the number of elements ahead of time (e.g., reading a stream of unknown length), fall back to the list‑conversion pattern.
  • Frequent resizing – If you must grow the array many times with small increments, the overhead of copying may outweigh the benefits of a single allocation. In such cases, periodically doubling the allocated capacity (a technique borrowed from dynamic arrays) can be a reasonable compromise.

Putting it all together – a practical template

def build_array(size: int, dtype=np.float32, method='prealloc'):
    """
    Build a 1‑D NumPy array using the chosen strategy.
    """
    if method == 'list':
        # List‑conversion pattern
        lst = [i * 2 for i in range(size)]
        return np.array(lst, dtype=dtype)

    elif method == 'prealloc':
        # Pre‑allocation with vectorised fill

```python
    elif method == 'prealloc':
        # Pre-allocation with vectorised fill
        arr = np.empty(size, dtype=dtype)
        # Write directly into the pre-allocated buffer to avoid creating
        # a temporary array and copying it back.
        idx = np.arange(size, dtype=dtype)
        np.multiply(idx, 2, out=arr)
        return arr

    else:
        raise ValueError(f"Unknown method: {method}")

Using the template

if __name__ == "__main__":
    n = 1_000_000

    preallocated = build_array(n, dtype=np.float32, method='prealloc')
    from_list    = build_array(n, dtype=np.float32, method='list')

    # Both produce identical values; the pre-allocated version avoids
    # the intermediate Python list and the subsequent copy.
    assert np.array_equal(preallocated, from_list)

In-place writes with out=

NumPy universal functions (ufuncs) such as np.multiply, np.But sin, and np. add, np.exp accept an out argument.

buffer = np.empty(n,

dtype=np.float32)
np.multiply(np.arange(n, dtype=np.float32), 2, out=buffer)

Because np.multiply writes directly into buffer, no intermediate array is allocated for the element-wise product. This is especially valuable when working with large arrays where temporary copies can double memory usage and increase cache pressure.

You can combine out= with broadcasting as well:

weights = np.linspace(0.1, 1.0, n, dtype=np.float32)
np.multiply(idx, weights, out=buffer)

If you need conditional updates, many ufuncs also accept

Just Went Up

Hot New Posts

Related Corners

A Few Steps Further

Thank you for reading about How To Append To Numpy Array. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home