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])
new_arr = np.append(arr, 4)
print("Single append:", new_arr)
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]])
new_row = np.array([[7, 8, 9]])
result = np.append(arr, new_row, axis=0)
print("New row:\n", result)
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]]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])
new_arr = np.insert(arr, 2, 99)
print("Insert at index 2:", new_arr)
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]])
new_row = [4, 5, 6]
result = np.insert(arr, 1, new_row, axis=0)
print("Insert row:\n", result)
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]]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])
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]])
new_rows = np.array([[5, 6], [7, 8]])
result = np.concatenate((arr, new_rows), axis=0)
print("Add rows:\n", result)
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]]Using np.hstack() and np.vstack()
These convenience functions simplify horizontal and vertical stacking:
import numpy as np
arr = np.array([1, 2, 3])
result = np.hstack((arr, [4, 5]))
print("hstack:", result)
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
Comparison of Methods
| Method | Position | Best For |
|---|---|---|
np.append() |
End of array | Adding values to the end |
np.insert() |
Any index | Adding values at specific positions |
np.concatenate() |
End (or start) | Combining multiple arrays efficiently |
np.hstack() |
Horizontal | Adding columns or extending 1D arrays |
np.vstack() |
Vertical | Adding 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
axisparameter 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.