Sunday, September 22, 2024
Google search engine
HomeLanguagesPython | Cloning or Copying a list

Python | Cloning or Copying a list

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
  • Using the map method 
  • Using slice() function

1. Using the 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. 

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] 

Time Complexity: O(n), where n is the length of slice
Auxiliary Space: O(1)

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]

Time Complexity: O(1)
Auxiliary Space: O(n), where n is length of list.

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 built-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]

9. Using enumerate function

Python3




# Python code to clone or copy a list
# Using list comprehension
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]

10. Using map function

Python3




# Python code to clone or copy a list
# Using map function
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

11. 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




# Python code to clone or copy a list
# Using slice() function
lst = [4, 8, 2, 10, 15, 18]
li_copy = lst[slice(len(lst))]
print("Original List:", lst)
print("After Cloning:", li_copy)
 
# This code is contributed by Pushpesh Raj.


Output

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

12. 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]

13. Using reduce  method():

  1. Import the reduce() function from the functools module.
  2. Define a function called clone_list() that takes a single argument li1, which is a list.
  3. Inside the function, call reduce() with the following arguments:
  4. A lambda function that takes two arguments x and y, where x is the accumulated list and y is the current element in li1. The lambda function returns the accumulated list with y appended to it.
    The input list li1.
  5. An empty list [] as the initial value of the accumulated list.
  6. Return the accumulated list from reduce().
  7. In the driver code, create a list li1.
  8. Call the clone_list() function with li1 as the argument and assign the result to a new list li2.
  9. Print the original list li1 and the cloned list li2.

Python3




from functools import reduce
 
def clone_list(li1):
    return reduce(lambda x, y: x + [y], li1, [])
 
# Driver Code
li1 = [4, 8, 2, 10, 15, 18]
li2 = clone_list(li1)
print("Original List:", li1)
print("After Cloning:", li2)
#This code is contributed by Jyothi pinjala.


Output

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

The time complexity : O(n), where n is the length of the input list. This is because the reduce() method iterates over the input list once to accumulate the cloned list.

The space complexity : O(n), because we create a new list li_copy that is the same length as the input list. However, since we don’t create any other significant new data structures or variables, the space complexity of this implementation is relatively low.

RELATED ARTICLES

Most Popular

Recent Comments