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.
Cloning or Copying a list
Below are the ways by which we can clone or copy a list in Python:
- 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
- Using the map method
- Using slice() function
Copy or Clone a List Using Slicing Technique
This is the easiest and 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. In this example, we are using List Slicing Technique to clone or copy a list.
Python3
def Cloning(li1):
li_copy = li1[:]
return li_copy
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]
Time Complexity: O(n), where n is the length of slice
Auxiliary Space: O(1)
Clone or Copy Using Python 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. In this example, we are using extend() to copy or clone list.
Python3
def Cloning(li1):
li_copy = []
li_copy.extend(li1)
return li_copy
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]
Python Copy List 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. In this example, we are using assignment operator to clone or copy Python list.
Python3
def Cloning(li1):
li_copy = li1
return li_copy
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]
Time Complexity: O(1)
Auxiliary Space: O(n), where n is length of list.
Using Shallow Copy in Python
This method of copying using copy. This takes around 0.186 seconds to complete. In this example, we are using Shallow Copy to copy or clone a list in Python.
Python3
import copy
li1 = [ 1 , 2 , [ 3 , 5 ], 4 ]
li2 = copy.copy(li1)
print (li2)
|
Output:
[1, 2, [3, 5], 4]
Python Cloning or Copying a list Using List Comprehension
In this example, we are using list comprehension to clone or copy a list.
Python3
def Cloning(li1):
li_copy = [i for i in li1]
return li_copy
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]
Python append() to Clone or Copy a list
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. In this example, we are using Python append to copy a Python list.
Python3
def Cloning(li1):
li_copy = []
for item in li1: li_copy.append(item)
return li_copy
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]
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.
Python3
def Cloning(li1):
li_copy = []
li_copy = li1.copy()
return li_copy
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]
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.
Python3
import copy
li1 = [ 1 , 2 , [ 3 , 5 ], 4 ]
li3 = copy.deepcopy(li1)
print (li3)
|
Output:
[1, 2, [3, 5], 4]
Copy a List Using Enumerate Function
In this example, we are using enumerate function to copy or clone a list.
Python3
lst = [ 4 , 8 , 2 , 10 , 15 , 18 ]
li_copy = [i for a,i in enumerate (lst)]
print ( "Original List:" , lst)
print ( "After Cloning:" , li_copy)
|
Output
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
Python Copy or Clone a List using Map Function
In this example, we are using map function to clone or copy a list using map function.
Python3
lst = [ 4 , 8 , 2 , 10 , 15 , 18 ]
li_copy = map ( int , lst)
print ( "Original List:" , lst)
print ( "After Cloning:" , * li_copy)
|
Output:
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: 4 8 2 10 15 18
Using slice() function
The Python slice() is an inbuilt method used to copy all the elements from one list to another. Here slice() function is passed with one argument which is an integer specifying at which position slicing will end.
Python3
lst = [ 4 , 8 , 2 , 10 , 15 , 18 ]
li_copy = lst[ slice ( len (lst))]
print ( "Original List:" , lst)
print ( "After Cloning:" , li_copy)
|
Output
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
Using the deque() function
The deque() function is used to create a double-ended queue in Python. It can be used to efficiently append or remove elements from both ends of the queue. In this approach, we create a deque object with the original list and then convert it back to a list to create a copy of the original list.
Python3
from collections import deque
original_list = [ 4 , 8 , 2 , 10 , 15 , 18 ]
copied_list = deque(original_list)
copied_list = list (copied_list)
print ( "Original List:" , original_list)
print ( "After Cloning:" , copied_list)
|
Output
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18]
Copy a Python List Using reduce()
In this example, we are using reduce() method to copy a list.
Python3
from functools import reduce
def clone_list(li1):
return reduce ( lambda x, y: x + [y], li1, [])
li1 = [ 4 , 8 , 2 , 10 , 15 , 18 ]
li2 = clone_list(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]
Time complexity : O(n), where n is the length of the input list.
Space complexity : O(n)
Please Login to comment...