copy() – copy list

The most direct way to copy a list in Python is via the copy() method.

Code Explanation

LineDescription
1Declaration and initialization of the list sourceList with the values 1, 2, 3, 4 and 5
3The sourceList is copied to the newly created targetList using the copy() method.
5 + 6With the for loop each list element of targetList is output.
sourceList = [1, 2, 3, 4, 5]

targetList = sourceList.copy()

for i in targetList:
    print(i, end=" ")
Output
1 2 3 4 5