Skip to content
Related Articles

Related Articles

Python | Cloning or Copying a list

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 28 Jun, 2022

In this article, we will go through various ways of copy lists in Python. These various ways of copying list take different execution times, so we can compare them on the basis of time. 

Different Ways to copy list:

  • Using the slicing technique 
  • Using the extend() method 
  • List copy using =(assignment operator)
  • Using the method of Shallow Copy 
  • Using list comprehension 
  • Using the append() method 
  • Using the copy() method 
  • Using the method of Deep Copy 

1. Using the slicing technique 

This is the easiest and the fastest way to clone a list. This method is considered when we want to modify a list and also keep a copy of the original. In this, we make a copy of the list itself, along with the reference. This process is also called cloning. This technique takes about 0.039 seconds and is the fastest technique. 

For more information, you can read this article – List Slicing Technique

Example:

Python3




# Python program to copy or clone a list
# Using the Slice Operator
def Cloning(li1):
    li_copy = li1[:]
    return li_copy
 
 
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)


Output: 

Original List: [4, 8, 2, 10, 15, 18] 
After Cloning: [4, 8, 2, 10, 15, 18] 

2. Using the extend() method 

The lists can be copied into a new list by using the extend() function. This appends each element of the iterable object (e.g., another list) to the end of the new list. This takes around 0.053 seconds to complete. 

For more information, you can read this article – extend()

Example:

Python3




# Python code to clone or copy a list
# Using the in-built function extend()
def Cloning(li1):
    li_copy = []
    li_copy.extend(li1)
    return li_copy
   
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)


Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

3. List copy using =(assignment operator)

This is the simplest method of cloning a list by using = operators. This operator assigns the old list to the new list using Python = operators. 

Here we will create a list and then we will copy the old list into the new list using assignment operators.

Note – There is an issue with this method if you modify in the new list then the old list is also modified due to the new list is referencing 

Example:

Python3




# Python code to clone or copy a list
# Using the List copy using =
def Cloning(li1):
    li_copy = li1
    return li_copy
   
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)


Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

4. Using the method of Shallow Copy 

This method of copying using copy. Shallow copy is well explained in the article Shallow Copy. This takes around 0.186 seconds to complete. 

Example:

Python3




# importing copy module
import copy
   
# initializing list 1
li1 = [1, 2, [3,5], 4]
   
# using copy for shallow copy 
li2 = copy.copy(li1)
 
print(li2)


Output:

[1, 2, [3, 5], 4]

5. Using list comprehension 

The method of list comprehension can be used to copy all the elements individually from one list to another. This takes around 0.217 seconds to complete. 

Example:

Python3




# Python code to clone or copy a list
# Using list comprehension
def Cloning(li1):
    li_copy = [i for i in li1]
    return li_copy
   
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)


Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

6. Using the append() method 

This can be used for appending and adding elements to list or copying them to a new list. It is used to add elements to the last position of the list. This takes around 0.325 seconds to complete and is the slowest method of cloning. 

Example:

Python3




# Python code to clone or copy a list
# Using append()
def Cloning(li1):
    li_copy =[]
    for item in li1: li_copy.append(item)
    return li_copy
   
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)


Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

7. Using the copy() method 

The Python List copy() is an inbuilt method copy used to copy all the elements from one list to another. This takes around 1.488 seconds to complete.

Example: 

Python3




# Python code to clone or copy a list
# Using bilt-in method copy()
def Cloning(li1):
    li_copy =[]
    li_copy = li1.copy()
    return li_copy
   
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = Cloning(li1)
print("Original List:", li1)
print("After Cloning:", li2)


Output: 

Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]

8. Using the method of Deep Copy 

This method of copying is well explained in the article Deep Copy. This takes around 10.59 seconds to complete and is the slowest method of cloning. 

Example:

Python3




# importing copy module
import copy
   
# initializing list 1
li1 = [1, 2, [3,5], 4]
 
# using deepcopy for deepcopy 
li3 = copy.deepcopy(li1)
print(li3)


Output:

[1, 2, [3, 5], 4]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!