numpy
array manipulation
add element
python
numpy arrays

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.

python
1import numpy as np
2
3arr = np.array([1, 2, 3])
4result = np.append(arr, 4)
5
6print(arr)
7print(result)

Output:

python
[1 2 3]
[1 2 3 4]

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.

python
1import numpy as np
2
3arr = np.array([1, 2, 3])
4result = np.concatenate([arr, np.array([4])])
5
6print(result)

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:

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4new_row = np.array([[5, 6]])
5
6result = np.append(arr, new_row, axis=0)
7print(result)

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:

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4new_col = np.array([[7], [8]])
5
6result = np.append(arr, new_col, axis=1)
7print(result)

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:

python
1import numpy as np
2
3arr = np.array([])
4for i in range(5):
5    arr = np.append(arr, i)
6
7print(arr)

This works, but it performs repeated reallocation and copying.

A better pattern is:

  1. collect values in a Python list
  2. convert once at the end
python
1import numpy as np
2
3values = []
4for i in range(5):
5    values.append(i)
6
7arr = np.array(values)
8print(arr)

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.

python
1import numpy as np
2
3arr = np.empty(5, dtype=int)
4
5for i in range(5):
6    arr[i] = i
7
8print(arr)

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:

python
1import numpy as np
2
3arr = np.array([1, 2, 3])
4print(np.append(arr, 4))

For 2D arrays, the inserted value usually needs to be shaped correctly:

python
1import numpy as np
2
3arr = np.array([[1, 2], [3, 4]])
4row = np.array([[5, 6]])
5print(np.append(arr, row, axis=0))

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.append is the simplest option for small one-off additions.'
  • 'np.concatenate expresses 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.

Course illustration
Course illustration

All Rights Reserved.