Python Program to Clone or Copy a List

Write a Python program to perform cloning or copying one list to another. In Python, we can use the list function to clone or copy a list.

# List Clone or Copy

orgList = [10, 20, 40, 60, 70]
print("Original List Items    = ", orgList)
print("Original List Datatype = ", type(orgList))

newList = list(orgList)
print("New List Items    = ", newList)
print("New List Datatype = ", type(newList))
Python Program to Clone or Copy a List 1

Python Program to Clone or Copy a List

In this Python list example, we slice the list without the start and stop value, which eventually returns the total list items.

# List Clone or Copy

orgList = [40, 60, 80, 100, 120]
print("Original List Items = ", orgList)

newList = orgList[:]
print("New List Items      = ", newList)

Python copy a list output

Original List Items =  [40, 60, 80, 100, 120]
New List Items      =  [40, 60, 80, 100, 120]

In this Python program, we used the list extend function to copy the list to another.

# List Clone or Copy

orgList = [22, 33, 44, 55, 66, 77, 88, 99]
print("Original List Items = ", orgList)

newList = []
newList.extend(orgList)
print("New List Items      = ", newList)

Python clone a list output

Original List Items =  [22, 33, 44, 55, 66, 77, 88, 99]
New List Items      =  [22, 33, 44, 55, 66, 77, 88, 99]

This Python program uses the list comprehension to clone our copy the list items.

# List Clone or Copy

orgList = [12, 23, 34, 45, 56, 67, 78]
print("Original List Items = ", orgList)

newList = [lVal for lVal in orgList]
print("New List Items      = ", newList)

copy a list using list comprehension output

Original List Items =  [12, 23, 34, 45, 56, 67, 78]
New List Items      =  [12, 23, 34, 45, 56, 67, 78]

We used the for loop (for lVal in orgList) and for loop range (for i in range(len(orgList))) to iterate the list items. Within the loops, the append function adds or appends each list item to the new list.

# List Clone or Copy

orgList = [25, 35, 45, 55, 65, 75, 85]
print("Original List Items = ", orgList)

newList = []

for lVal in orgList:
    newList.append(lVal)
print("New List Items      = ", newList)

newList2 = []

for i in range(len(orgList)):
    newList2.append(orgList[i])
print("New List Items      = ", newList2)
Original List Items =  [25, 35, 45, 55, 65, 75, 85]
New List Items      =  [25, 35, 45, 55, 65, 75, 85]
New List Items      =  [25, 35, 45, 55, 65, 75, 85]

In Python, there is a built-in list copy function to copy or clone the list items.

# List Clone or Copy

orgList = [24, 44, 54, 64, 76, 85]
print("Original List Items = ", orgList)

newList = orgList.copy()
print("New List Items      = ", newList)
Original List Items =  [24, 44, 54, 64, 76, 85]
New List Items      =  [24, 44, 54, 64, 76, 85]