Python Array Copying: Assignment, Shallow Copy, and Deep Copy
Learn how to effectively copy arrays in Python, understanding the differences between assignment, shallow copy, and deep copy. This guide explains how to create independent array copies and avoid common pitfalls.
Array Copying in Python
When working with arrays in Python, it's important to know how to copy them. Copying an array can be done in several ways: by using the assignment operator, shallow copy, and deep copy.
Assigning the Array
We can copy an array by using the assignment operator (=
). However, it doesn't create a new array, but rather a reference to the original array.
Example: Assigning the Array
from numpy import *
# Creating the first array
arr1 = array([2, 6, 9, 4])
# Displaying the identity of arr1
print(id(arr1))
# Assigning arr1 to arr2
arr2 = arr1
# Displaying the identity of arr2
print(id(arr2))
# Making a change in arr1
arr1[1] = 7
# Displaying the arrays
print(arr1)
print(arr2)
Output
117854800
117854800
[2 7 9 4]
[2 7 9 4]
Shallow Copy
A shallow copy creates a new array, but the elements themselves are references to the same objects as in the original array. Changes in the shallow copy may affect the original array.
Example: Shallow Copy using view()
from numpy import *
# Creating the first array
arr1 = array([2, 6, 9, 4])
# Displaying the identity of arr1
print(id(arr1))
# Shallow copy arr1 in arr2 using view()
arr2 = arr1.view()
# Displaying the identity of arr2
print(id(arr2))
# Making a change in arr1
arr1[1] = 7
# Displaying the arrays
print(arr1)
print(arr2)
Output
[2 7 9 4]
[2 7 9 4]
Deep Copy
A deep copy creates a new array and recursively copies the elements. Changes made to the copied array do not affect the original array.
Example: Deep Copy using copy()
from numpy import *
# Creating the first array
arr1 = array([2, 6, 9, 4])
# Displaying the identity of arr1
print(id(arr1))
# Deep copy arr1 in arr2 using copy()
arr2 = arr1.copy()
# Displaying the identity of arr2
print(id(arr2))
# Making a change in arr1
arr1[1] = 7
# Displaying the arrays
print(arr1)
print(arr2)
Output
[2 7 9 4]
[2 6 9 4]
Deep Copy for Nested Structures
For nested lists or matrices, we can use deepcopy()
from the copy
module to create a deep copy that prevents changes in the copy from affecting the original.
Example: Rotating a Matrix
import copy
def rotate_matrix(image):
# Copy method one
copy_image_one = copy.deepcopy(image)
print("Original", image)
print("Copy of original", copy_image_one)
N = len(image)
# Reverse order within each row
for row in range(N):
for column in range(N):
copy_image_one[row][column] = image[row][N-column-1]
print("After modification")
print("Original", image)
print("Copy", copy_image_one)
# Copy method two
copy_image_two = [list(row) for row in copy_image_one]
# Transpose
for row in range(N):
for column in range(N):
copy_image_two[column][row] = copy_image_one[row][column]
return copy_image_two
if __name__ == "__main__":
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("Rotated image", rotate_matrix(matrix))
Output
Original [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copy of original [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
After modification
Original [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copy [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
Rotated image [[3, 6, 9], [2, 5, 8], [1, 4, 7]]
Key Takeaways
- Assigning: Creates a reference, not a new array.
- Shallow Copy: Copies references, changes in one may affect the other.
- Deep Copy: Creates a completely independent copy, changes in one do not affect the other.
- Use
deepcopy()
for nested structures like matrices to ensure full independence between original and copied arrays.