How to Make a Copy of an Array in Python 


               


How to Make a Copy of an Array in Python



Python


In this article, we show how to make a copy, or a duplicate, of an array in Python.

Using the Python copy() function, we can make a copy of an array and the copied array will contain all of the same exact elements as the original array.

So in the code below, we create an array and then we make a copy of this array.



So, first, we must import numpy as np, since we are using numpy to create an array.

Next, we create an array, called originalarray, that goes from 0 to 5, so an array of 6 elements.

We show the contents of this array, an array that goes from 0 to 5.

We tehn create another variable, copiedarray, and set it equal to, originalarray.copy()

We then show the contents of this copiedarray, which is composed of the same exact elements of the original array.

In other words, we have created a copy, or a duplicate, of the original array.

Now if you were to make changes to one of the arrays, it would not affect the other array, because after this point, both arrays are independent of each other.

And this is how you can create a copy of an array in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...