OneBite.Dev - Coding blog in a bite size

copy an array in python

Code snippet on how to copy an array in python

myArray = [1,2,3,4,5]
myNewArray = myArray.copy()

The above code is written in Python to copy an array. First, a variable called myArray is declared with a list of integers as its value. Then a new variable called myNewArray is declared with the value of the myArray variable, which is the same list of integers. The .copy() method is used to create a shallow copy of myArray and assign it to the myNewArray variable. This way, the two arrays are now independent of each other, and we can make changes to one array without affecting the other. This code is a simple and fast way to copy an array in Python.

python