Skip to main content

Python NumPy: How to Add a New Value to a NumPy Array in Python

Unlike standard Python lists where you can simply call .append(), NumPy arrays have a fixed size once created. Adding elements requires creating a new array that includes the additional values. NumPy provides several functions for this, each suited for different scenarios - appending to the end, inserting at a specific position, or combining multiple arrays.

This guide covers the main approaches with examples for both 1D and 2D arrays.

Using np.append() - Add to the End​

The np.append() function adds one or more values to the end of an array. It returns a new array - the original is not modified.

import numpy as np

arr = np.array([1, 2, 3])

# Append a single value
new_arr = np.append(arr, 4)
print("Single append:", new_arr)

# Append multiple values
new_arr = np.append(arr, [4, 5, 6])
print("Multiple append:", new_arr)

Output:

Single append: [1 2 3 4]
Multiple append: [1 2 3 4 5 6]

Appending to a 2D Array​

For 2D arrays, specify the axis parameter to control whether you add a row or a column:

import numpy as np

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

# Append a new row (axis=0)
new_row = np.array([[7, 8, 9]])
result = np.append(arr, new_row, axis=0)
print("New row:\n", result)

# Append a new column (axis=1)
new_col = np.array([[10], [11]])
result = np.append(arr, new_col, axis=1)
print("New column:\n", result)

Output:

New row:
[[1 2 3]
[4 5 6]
[7 8 9]]
New column:
[[ 1 2 3 10]
[ 4 5 6 11]]
Without axis, the array is flattened

If you omit the axis parameter on a 2D array, np.append() flattens both arrays into 1D before appending:

import numpy as np

arr = np.array([[1, 2], [3, 4]])
result = np.append(arr, [5, 6]) # No axis specified

print(result)
# Output: [1 2 3 4 5 6] (flattened!)

Always specify axis when working with multi-dimensional arrays to preserve the shape.

Using np.insert() - Add at a Specific Position​

The np.insert() function lets you insert values at any index within the array, not just at the end:

import numpy as np

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

# Insert value 99 at index 2
new_arr = np.insert(arr, 2, 99)
print("Insert at index 2:", new_arr)

# Insert multiple values at index 1
new_arr = np.insert(arr, 1, [10, 20])
print("Insert multiple at index 1:", new_arr)

Output:

Insert at index 2: [ 1  2 99  3  4  5]
Insert multiple at index 1: [ 1 10 20 2 3 4 5]

Inserting Rows and Columns in 2D Arrays​

import numpy as np

arr = np.array([[1, 2, 3],
[7, 8, 9]])

# Insert a new row at index 1 (between existing rows)
new_row = [4, 5, 6]
result = np.insert(arr, 1, new_row, axis=0)
print("Insert row:\n", result)

# Insert a new column at index 1
new_col = [10, 20]
result = np.insert(arr, 1, new_col, axis=1)
print("Insert column:\n", result)

Output:

Insert row:
[[1 2 3]
[4 5 6]
[7 8 9]]
Insert column:
[[ 1 10 2 3]
[ 7 20 8 9]]
tip

np.insert() is the only method that lets you add values at arbitrary positions. Use it when you need precise control over placement.

Using np.concatenate() - Combine Arrays​

The np.concatenate() function joins two or more arrays along an existing axis. It's the most efficient choice when combining larger blocks of data:

import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5])
arr3 = np.array([6, 7, 8])

# Concatenate multiple arrays
result = np.concatenate((arr1, arr2, arr3))
print("Concatenated:", result)

Output:

Concatenated: [1 2 3 4 5 6 7 8]

Concatenating 2D Arrays​

import numpy as np

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

# Add rows (axis=0)
new_rows = np.array([[5, 6], [7, 8]])
result = np.concatenate((arr, new_rows), axis=0)
print("Add rows:\n", result)

# Add columns (axis=1)
new_cols = np.array([[10, 20], [30, 40]])
result = np.concatenate((arr, new_cols), axis=1)
print("Add columns:\n", result)

Output:

Add rows:
[[1 2]
[3 4]
[5 6]
[7 8]]
Add columns:
[[ 1 2 10 20]
[ 3 4 30 40]]
note

np.concatenate() requires all arrays to have the same number of dimensions and matching shapes along all axes except the concatenation axis. For stacking arrays with different dimensions, use np.vstack() or np.hstack() instead.

Using np.hstack() and np.vstack()​

These convenience functions simplify horizontal and vertical stacking:

import numpy as np

arr = np.array([1, 2, 3])

# Horizontal stack (add to the right)
result = np.hstack((arr, [4, 5]))
print("hstack:", result)

# Vertical stack (add as a new row)
arr2d = np.array([[1, 2, 3]])
result = np.vstack((arr2d, [[4, 5, 6]]))
print("vstack:\n", result)

Output:

hstack: [1 2 3 4 5]
vstack:
[[1 2 3]
[4 5 6]]

Performance Considerations​

Avoid adding elements in a loop

Every time you use np.append(), np.insert(), or np.concatenate(), NumPy creates a brand-new array and copies all existing data into it. This makes repeated additions in a loop very slow - O(n²) overall.

Wrong approach (slow):​

import numpy as np

arr = np.array([])
for i in range(10000):
arr = np.append(arr, i) # Creates a new array every iteration!

Correct approach (fast):​

import numpy as np

# Option 1: Build a Python list first, then convert
values = list(range(10000))
arr = np.array(values)

# Option 2: Pre-allocate the array
arr = np.empty(10000)
for i in range(10000):
arr[i] = i

The list-based approach is typically 100x faster than repeated np.append() calls.

Comparison of Methods​

MethodPositionBest For
np.append()End of arrayAdding values to the end
np.insert()Any indexAdding values at specific positions
np.concatenate()End (or start)Combining multiple arrays efficiently
np.hstack()HorizontalAdding columns or extending 1D arrays
np.vstack()VerticalAdding rows to 2D arrays

Conclusion​

Adding values to NumPy arrays requires creating new arrays because NumPy arrays have fixed sizes.

  • Use np.append() for simple additions to the end,
  • Use np.insert() when you need to place values at specific indices
  • Use np.concatenate() for combining larger arrays efficiently.
  • For 2D operations, always specify the axis parameter to avoid unintended flattening.

Most importantly, avoid adding elements in a loop - build a Python list first and convert it to a NumPy array for the best performance.