Python | Cloning or Copying a list
In this article we will go through various ways of copying or cloning a list in Python. These various ways of copying takes different execution time, so we can compare them on the basis of time.
- Using 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.# Python program to copy or clone a list# Using the Slice OperatordefCloning(li1):li_copy=li1[:]returnli_copy# Driver Codeli1=[4,8,2,10,15,18]li2=Cloning(li1)print("Original List:", li1)print("After Cloning:", li2)chevron_rightfilter_noneOutput:
Original List: [4, 8, 2, 10, 15, 18]
After Cloning: [4, 8, 2, 10, 15, 18] - 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., anothre list) to the end of the new list. This takes around 0.053 second to complete.
Example:# Python code to clone or copy a list# Using the in-built function extend()defCloning(li1):li_copy=[]li_copy.extend(li1)returnli_copy# Driver Codeli1=[4,8,2,10,15,18]li2=Cloning(li1)print("Original List:", li1)print("After Cloning:", li2)chevron_rightfilter_noneOutput:
Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]
- Using the list() method
This is the simplest method of cloning a list by using the builtin function list(). This takes about 0.075 seconds to complete.
Example:# Python code to clone or copy a list# Using the in-built function list()defCloning(li1):li_copy=list(li1)returnli_copy# Driver Codeli1=[4,8,2,10,15,18]li2=Cloning(li1)print("Original List:", li1)print("After Cloning:", li2)chevron_rightfilter_noneOutput:
Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]
- Using the method of Shallow Copy
This method of copying using copy.copy is well explained in the article Shallow Copy. This takes around 0.186 seconds to complete. - 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.# Python code to clone or copy a list# Using list comprehensiondefCloning(li1):li_copy=[iforiinli1]returnli_copy# Driver Codeli1=[4,8,2,10,15,18]li2=Cloning(li1)print("Original List:", li1)print("After Cloning:", li2)chevron_rightfilter_noneOutput:
Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]
- 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 list. This takes around 0.325 seconds to complete and is the slowest method of cloning.# Python code to clone or copy a list# Using append()defCloning(li1):li_copy=[]foriteminli1: li_copy.append(item)returnli_copy# Driver Codeli1=[4,8,2,10,15,18]li2=Cloning(li1)print("Original List:", li1)print("After Cloning:", li2)chevron_rightfilter_noneOutput:
Original List: [4, 8, 2, 10, 15, 18] After Cloning: [4, 8, 2, 10, 15, 18]
- Using the copy() method
The inbuilt method copy is used to copy all the elements from one list to another. This takes around 1.488 seconds to complete.
Example:# Python code to clone or copy a list# Using bilt-in method copy()defCloning(li1):li_copy=[]li_copy=li1.copy()returnli_copy# Driver Codeli1=[4,8,2,10,15,18]li2=Cloning(li1)print("Original List:", li1)print("After Cloning:", li2)chevron_rightfilter_noneOutput:
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.
Referred to Stack Overflow.
Recommended Posts:
- Python | Difference between Pandas.copy() and copying through variables
- Python | Convert list of tuples to list of list
- Python | Convert list of string to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Find maximum length sub-list in a nested list
- Python | Convert list of string into sorted list of integer
- Python | Add list elements with a multi-list based on index
- Python | Convert list of numerical string to list of Integers
- Python | Pair and combine nested list to tuple list
- Python | Replace elements in second list with index of same element in first list
- Python | Filter list of strings based on the substring list
- Python | Merge list of tuple into list by joining the strings
- Python | Merge List with common elements in a List of Lists
- Python | Sorting list of lists with similar list elements
- Python | Filter a list based on the given list of strings
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



