How do I deep copy lists in Python using NumPy?

In Python, you can use NumPy to create deep copies of lists efficiently. Deep copying is especially useful when you need to duplicate a list that contains other lists (or nested structures) to ensure that changes to the copied list do not affect the original list.

import numpy as np # Original nested list original_list = [[1, 2, 3], [4, 5, 6]] # Deep copy using NumPy copied_list = np.copy(original_list) # Modify the copied list copied_list[0][0] = 99 print("Original List:", original_list) # Output: [[1, 2, 3], [4, 5, 6]] print("Copied List:", copied_list) # Output: [[99, 2, 3], [4, 5, 6]]

deep copy numpy Python lists deep copy lists copy nested lists