HOWTO · Python
How to Copy a 2D Array in Python
Learn when to use NumPy copy(), copy.deepcopy(), a shallow list copy, or a NumPy view for 2D data in Python.
On this page
To copy 2D data in Python, choose the operation for its type and sharing behavior: use NumPy’s copy() for independent array data, copy.deepcopy() for a nested list whose inner lists must also be independent, or a shallow copy when sharing inner objects is intentional. A NumPy view() is a distinct array object that shares its underlying data with the original.
A 2D array organizes values into rows and columns. In a Python list of lists, each inner list is one row; two indices select a row and then an element in that row, as in data[row_index][column_index].
Create a Copy of 2D Arrays Using the NumPy copy() Method
For a NumPy ndarray, call its copy() method to create an array with its own data. This example changes one value in the copy and then inspects both arrays:
import numpy as np
original = np.array([[23, 34, 45], [24, 45, 78]])
copied = original.copy()
copied[0, 2] = 65
print("Copy:", copied.tolist())
print("Original:", original.tolist())
Copy: [[23, 34, 65], [24, 45, 78]]
Original: [[23, 34, 45], [24, 45, 78]]
The assignment changes copied; the original array retains its initial values. This is the direct choice when you need to edit numeric array data independently. NumPy arrays can also contain Python objects; copying an object-dtype array copies the array’s data buffer but does not recursively copy the Python objects stored in it.
Create a Copy of a 2D Array Using the copy.deepcopy() Function
For a nested Python list, copy.deepcopy() recursively copies the lists and their contents. This matters when an inner list may be mutated after copying:
import copy
original = [["red", "blue"], ["green", "gold"]]
copied = copy.deepcopy(original)
copied[0].append("white")
print("Copy:", copied)
print("Original:", original)
Copy: [['red', 'blue', 'white'], ['green', 'gold']]
Original: [['red', 'blue'], ['green', 'gold']]
The new outer list and its inner lists are separate from the original, so appending to an inner list in copied leaves original unchanged. deepcopy() is useful for nested mutable data; for large or custom objects, consider whether recursively copying every referenced object is appropriate.
Create a Shallow Copy of a 2D Array in Python
A shallow copy creates a new outer container while keeping references to the original’s inner objects. For a nested list, copy.copy() and the outer-list method list.copy() have this behavior:
import copy
original = [["red", "blue"], ["green", "gold"]]
copied = copy.copy(original)
copied[0].append("white")
print("Copy:", copied)
print("Original:", original)
Copy: [['red', 'blue', 'white'], ['green', 'gold']]
Original: [['red', 'blue', 'white'], ['green', 'gold']]
Here, both outer lists refer to the same first inner list, so the append is visible through both. Replacing an item in the copied outer list, such as copied[0] = ["black"], changes only that outer list’s reference. Use copy.deepcopy() when the inner lists also need to be independent.
NumPy’s view() is a different operation from a shallow copy of a nested Python list. It returns a distinct ndarray object that shares the original array’s data, so assigning through the view changes the values seen through the original as well. For independent NumPy data, use ndarray.copy() instead. Object identity and data sharing are separate questions: two arrays can be different objects while still exposing the same underlying values.
import numpy as np
original = np.array([[24, 23, 25], [32, 43, 54]])
view = original.view()
view[0, 2] = 100
print("View:", view.tolist())
print("Original:", original.tolist())
View: [[24, 23, 100], [32, 43, 54]]
Original: [[24, 23, 100], [32, 43, 54]]