Let us see how to copy arrays in Python. There are 3 ways to copy arrays :
- Simply using the assignment operator.
- Shallow Copy
- Deep Copy
Assigning the Array
We can create a copy of an array by using the assignment operator (=).
new_arr = old_ arr
In Python, Assignment statements do not copy objects, they create bindings between a target and an object. When we use = operator user thinks that this creates a new object; well, it doesn’t. It only creates a new variable that shares the reference of the original object.
117854800 117854800 [2 7 9 4] [2 7 9 4]
We can see that both the arrays reference to the same object.
A shallow copy means constructing a new collection object and then populating it with references to the child objects found in the original. The copying process does not recurse and therefore won’t create copies of the child objects themselves. In the case of shallow copy, a reference of the object is copied in other object. It means that any changes made to a copy of the object do reflect in the original object. We will be implementing shallow copy using the
This time although the 2 arrays reference different objects, but still on changing the value of one, the value of another also changes.
Deep copy is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In case of deep copy, a copy of object is copied in other object. It means that any changes made to a copy of object do not reflect in the original object. We will be implementing deep copy using the
121258976 125714048 [2 7 9 4] [2 6 9 4]
This time the changes made in one array is not reflected in the another array.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Cloning or Copying a list
- Python | Difference between Pandas.copy() and copying through variables
- MoviePy – Shallow copying Video File Clip
- Python | Ways to convert array of strings to array of floats
- Python | Flatten a 2d numpy array into 1d array
- Python | Multiply 2d numpy array corresponding to 1d array
- Python - Built-in array vs NumPy array
- Python | Convert list to Python array
- Select an element or sub array by index from a Numpy Array
- How to Copy NumPy array into another array?
- Array in Python | Set 2 (Important Functions)
- Count frequencies of all elements in array in Python using collections module
- Python Slicing | Reverse an array in groups of given size
- Move all zeroes to end of array using List Comprehension in Python
- Program to print all distinct elements of a given integer array in Python | Ordered Dictionary
- Program to cyclically rotate an array by one in Python | List Slicing
- Python | Reverse an array upto a given position
- Prefix sum array in Python using accumulate function
- Python Set difference to find lost element from a duplicated array
- Python heapq to find K'th smallest element in a 2D array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.