How to Clone or Copy a List in Python (Shallow/Deep)

In this article, you will learn various ways to clone or copy a list in Python. All the comparisons are based on the speed by which you can copy or clone the list. Each method has a different speed of execution and you can pick the one that suits you better.

1. Using Append Method of List in Python

If you are already familiar with the append method of the list in python then you might know that append is used to add the elements in the list. Hence using this method you can add or append the element of one list to the copy list that you want to create.

#Python Code to copy the List
#With help of Append Method for List

#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Going through each element and appending it.
    for element in inputList:
        copyList.append(element)
    
    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [1,2,3,4,5,7]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [1, 2, 3, 4, 5, 7]
Copied/Cloned List:  [1, 2, 3, 4, 5, 7]

2. Using Deep Copy Method of Python

You can use the deep copy method of copying the list in Python. A deep copy creates a new compound object and then, recursively, inserts copies of the objects discovered in the original object into the new compound object.[1]

To avoid difficulties, the deepcopy() function keeps track of objects that have previously been copied during the current copying pass and allows user-defined classes to override either the copying operation or the list of components that have been copied in the current copying run.

Let us see the Deep Copy of List in Action in Python in the below code. The below method shows Python List Copy.

#Python Code to copy the List
#With help of Deep Copy Method for List
#importing Copy Libray to get Shallow and Deep copy features

import copy
#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing Deep Copy on the List
    copyList = copy.deepcopy(inputList)

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [5,12,45,14,67,13,54]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [5, 12, 45, 14, 67, 13, 54]
Copied/Cloned List:  [5, 12, 45, 14, 67, 13, 54]

3. Using Shallow Copy Method Of Python

You can use the Shallow Copy method in python to copy the list. Shallow copy creates a new compound object and then (to the degree possible) add references to the objects present in the original object in that object.

Shallow copies of the List can be made by calling the copy.copy() method on the list. Let us see in the below example code how shallow copy works in python.

#Python Code to copy the List
#With help of Shallow Copy Method for List
#importing Copy Libray to get Shallow and Deep copy features

import copy
#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing Shallow Copy on the List
    copyList = copy.copy(inputList)

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [4,12,7,1,9,2]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [4, 12, 7, 1, 9, 2]
Copied/Cloned List:  [4, 12, 7, 1, 9, 2]

4. Using copy method present in List Class of Python

The list class of Python consists of a copy function or method that helps you copy the list to a new list. It works similar to shallow copy as explained above. Let us see below Python Program by running how the normal copy function works in Python for List.

#Python Code to copy the List
#With help of Copy Method for List

#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing Copy Method on the List
    copyList = inputList.copy()

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [7,6,4,5,1]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [7, 6, 4, 5, 1]
Copied/Cloned List:  [7, 6, 4, 5, 1]

5. Using Slicing Method of List In Python

This is the quickest and most convenient method of cloning a list. This strategy is used when we wish to make changes to a list while also maintaining a copy of the original list. In this step, we create a copy of the list itself, as well as a reference to it. This procedure is referred to as cloning in some circles.

Below is the example code using the Slicing Technique of List in Python. Note that this is the fastest way to create a copy of the list in Python is recommended whenever you need a copy of the list.

#Python Code to copy the List
#With help of Slicing Method for List

#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing Slicing Method on the List
    copyList = inputList[:]

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [1,2,3,4,5]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [1, 2, 3, 4, 5]
Copied/Cloned List:  [1, 2, 3, 4, 5]

6. Using List Method Present in Python

You can use the list() function to create a copy of the input list. This method is also very fast as compared to the shallow or deep copy method mentioned above. In the below example code we will see how you can copy a list using the list function of Python.

#Python Code to copy the List
#With help of List Method for List

#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing List Method on the List
    copyList = list(inputList)

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [1,2,3,4,5]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

How to Clone or Copy a List in Python (Shallow Deep)

7. Using Extend Function to Copy a List in Python

To copy a list in Python you can use the extend function present in the list class. Extend method copies all the elements of the list one by one to new list iteratively and making this function to second best and fastest method to copy a list.

#Python Code to copy the List
#With help of Extend Method for List

#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing Extend Method on the List
    copyList.extend(inputList)

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [5,6,7,8,9]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [5, 6, 7, 8, 9]
Copied/Cloned List:  [5, 6, 7, 8, 9]

8. Using Append Method With Different Approach

You can loop through all the elements within the append function or method of Python and add each element in the copy list one by one. This method is also referred to as the comprehension method. Let us see in the example code below how to copy a list in Python using the list comprehension method.

#Python Code to copy the List
#With help of List Append Method for List

#Creating a function to return cloned or copy list
def getClonedOrCopyList(inputList):
    #initializing Copy List
    copyList = []

    #Performing List Append on the List
    copyList = [element for element in inputList]

    return copyList

#The Main Driver Code to get the Clone or Copy
inputList = [10,11,12,13,14,15]
copyList = getClonedOrCopyList(inputList)

#Printing the Copied List
print("Original List: ", inputList)
print("Copied/Cloned List: ", copyList)

Output:

Original List:  [10, 11, 12, 13, 14, 15]
Copied/Cloned List:  [10, 11, 12, 13, 14, 15]

That is all for the tutorial related to how to copy a list in python. Or how you can close the list in python. If you have any questions or issues feel free to ask in the comment section.

Please follow us on Facebook and Twitter. We will be soon coming with Free Courses on Python on all the topics such as Beginner, Intermediate, and Advance. Keep following us for further updates. Also, signup for the below newsletter to keep posted once we launch the courses.

Further Read:

  1. JSONPath in Python How to Parse With Examples
  2. Python Check If File is Empty – 4 Ways
  3. How To Round in Python [Complete Guide]
Share your love

3 Comments

Leave a Reply