Add single element to array in numpy
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
NumPy arrays are fixed-size containers, so "adding one element" really means creating a new array with the old contents plus the new value. That is why the correct answer is usually not only which function to call, but also whether NumPy is the right place to do repeated appends at all.
The simplest option: np.append
For a one-dimensional array, the most direct approach is numpy.append.
Output:
This is important: np.append does not modify arr in place. It returns a new array.
That behavior surprises people who are thinking in terms of Python lists.
np.concatenate is often clearer
Under the hood, appending is just a special case of concatenation. Many developers prefer np.concatenate because it makes the "new array from two arrays" idea more explicit.
This is especially useful when the new data is already available as an array rather than as a single scalar.
Appending to 2D arrays needs axis awareness
For multidimensional arrays, shape matters. Suppose you want to add one row:
If you forget axis=0, NumPy flattens the arrays before appending, which is usually not what you want.
Adding one column works similarly, but the new element must match the target shape:
The new column has shape (2, 1), which matches the number of rows in the original array.
Why repeated appends are a bad pattern
The fixed-size nature of NumPy arrays means every append creates a new array and copies data. Doing that in a loop is inefficient.
Bad pattern:
This works, but it performs repeated reallocation and copying.
A better pattern is:
- collect values in a Python list
- convert once at the end
If the final size is known in advance, preallocating is even better.
Preallocate when the size is known
If you know how many elements you need, create the array first and fill it by index.
This avoids repeated copying and is much closer to how NumPy is intended to be used for performance-sensitive code.
Scalar versus array shape
Another common issue is mixing scalars and arrays carelessly. For 1D arrays, appending a scalar is fine:
For 2D arrays, the inserted value usually needs to be shaped correctly:
If the shape does not match the selected axis, NumPy raises an error.
Common Pitfalls
The biggest mistake is assuming np.append mutates the original array. It does not. You must use the returned array.
Another issue is repeatedly appending in a loop. That is convenient for tiny examples and inefficient for real numerical workloads.
Developers also forget that np.append flattens by default when axis is omitted on multidimensional arrays. That often destroys the structure of the data.
Finally, shape mismatches are common when adding rows or columns to 2D arrays. Always check the array dimensions before appending along an axis.
Summary
- NumPy arrays are fixed-size, so adding an element creates a new array.
- '
np.appendis the simplest option for small one-off additions.' - '
np.concatenateexpresses the same idea more explicitly.' - For 2D arrays, specify the axis and match the inserted shape correctly.
- For repeated additions, use a Python list or preallocate instead of appending in a loop.

