AppDividend
Latest Code Tutorials

How to Copy an Array in Python

Copying one item from another is a careful operation if performed poorly, then it has its consequences. In programming, you are not just copying values, and sometimes you are also copying references. Let’s see how to copy an array in Python.

Python array copy

To copy an array in Python, use the assignment operator(=). But there are two more ways that you can use to copy the array.

  1. Shallow copy
  2. Deep copy

In Python, the assignment operator does not copy objects. Instead, it creates bindings between a target and an object. When we use the = operator, we think it creates a new object, but it doesn’t. It just creates a new variable that shares the reference of the original object.

To illustrate this, let’s take an example and see what I am talking about.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])

# Printing the id of the array
print(id(arr))

# Using assignment operator to copy array
arr2 = arr

# Printing the id of the copied array
print(id(arr2))

# Changing the original array
arr[2] = 11

# Printing both arrays
print(arr)
print(arr2)

Output

4365624976
4365624976
[ 1 2 11 4 5]
[ 1 2 11 4 5]

You can see that we copied the arr2 from arr, but both array objects share the same reference. So when you modify the original array, the change will apply to copied array too. So, if you change the arr, the arr2 will change too.

Now, let’s talk about shallow and deep copy in Python.

Shallow copy in Python

A shallow copy is a bit-wise copy of the object. What it means is that when a new object is created based on the original object, it has an exact copy of the values which is in the original object. If any of the values of the object are references to other objects, just the reference addresses are copied.

The copying process does not create copies of the child objects themselves. But, in the case of a shallow copy of an object, a reference of the object is copied to another object. It means that any changes made to a copy of the object do reflect in the original object.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])

# Printing the id of the array
print(id(arr))

# Shallow copy to copy array
arr2 = arr.view()

# Printing the id of the copied array
print(id(arr2))

# Changing the original array
arr[2] = 11

# Printing both arrays
print(arr)
print(arr2)

Output

4378519184
4378519280
[ 1 2 11 4 5]
[ 1 2 11 4 5]

You can see that two arrays reference different objects that means changing the value; the value of another also changes.

Let’s take a look at the deep copy in Python.

Deep copy in Python

Deep copy is a process in which the copying process occurs recursively. It means first creating a new object and then recursively populating it with copies of the child objects found in the original. In the case of deep copy, a copy of the object is copied into other objects.

So when you copy an object, the change in the value of the copied array does not reflect the original array.

To create a deep copy of an array in Python, use the array.copy() method. The array.copy() method does not take any argument because it is called on the original array and returns the deep copied array.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])

# Printing the id of the array
print(id(arr))

# Deep copy using copy() method
arr2 = arr.copy()

# Printing the id of the copied array
print(id(arr2))

# Changing the original array
arr[2] = 11

# Printing both arrays
print(arr)
print(arr2)

Output

4301858448
4301858544
[ 1 2 11 4 5]
[1 2 3 4 5]

Now you can see that the change in the value of the copied array does not reflect the original array. Both values and references are now entirely different, and this is called a deep copy of an array.

When you copy one array to another, most of the time, please use the copy() method because it uses the deep copy to copy the array; otherwise, you will the unwanted results.

Conclusion

To create a deep copy of an array in Python, use the array.copy() method provided in numpy API. To create a shallow copy of an array, use the array.view() method. Otherwise, you can create a copy of the object by using a simple assignment operator.

That’s it for this tutorial.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.