Open In App

Python | Cloning or Copying a list

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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)

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




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

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 operatorsHere 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




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

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




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

Python Cloning or Copying a list Using List Comprehension 

In this example, we are using list comprehension to clone or copy a list.

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]

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




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

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




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

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




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

Copy a List Using Enumerate Function

In this example, we are using enumerate function to copy or clone a list.

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]

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




# 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

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]

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, [])
 
# 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]

Time complexity : O(n), where n is the length of the input list.
Space complexity : O(n)



Previous Article
Next Article

Similar Reads

Python | Difference between Pandas.copy() and copying through variables
Pandas .copy() method is used to create a copy of a Pandas object. Variables are also used to generate copy of an object but variables are just pointer to an object and any change in new data will also change the previous data. The following examples will show the difference between copying through variables and Pandas.copy() method. Example #1: Co
2 min read
Array Copying in Python
Let us see how to copy arrays in Python. There are 3 ways to copy arrays : Simply using the assignment operator.Shallow CopyDeep CopyAssigning the Array We can create a copy of an array by using the assignment operator (=). Syntax : new_arr = old_ arr In Python, Assignment statements do not copy objects, they create bindings between a target and an
4 min read
Cloning Row and Column Vectors in Python
In Python, cloning row or column vectors involves creating a duplicate copy of a one-dimensional array (vector) either horizontally (row) or vertically (column). Cloning vectors is important for preserving data integrity and avoiding unintended modifications to the original array. In this article, we will explore different approaches to clone row o
2 min read
MoviePy – Shallow copying Video File Clip
In this article we will see how we can shallow copy the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the o
2 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of Python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let's discuss certain ways in which we can convert a list of tuples to list of list. Method #1: Using list compr
8 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python | Maximum sum of elements of list in a list of lists
Given lists in a list, find the maximum sum of elements of list in a list of lists. Examples: Input : [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output : 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input : [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Outpu
4 min read
Sort the values of first list using second list in Python
Given two lists, sort the values of one list using the second list. Examples: Input : list1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]Output : ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g'] Input : list1 = ["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"] list2 = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]O
6 min read
Python List Comprehension | Segregate 0's and 1's in an array list
You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Examples: Input : arr = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output : [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] We have existing solution for this problem please refer Segregate 0s and 1s in an array link. We can solve this problem quickly in Python usi
2 min read
Practice Tags :
three90RightbarBannerImg