Python Program To Cloning or Copying a list
In this tutorial, you will learn to clone or copy a list in Python. The List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index.
Cloning or copying a list is simply creating another list that has the same elements as the original list. Elements in a list can be copied into another list by various methods, we will be discussing most of them in this tutorial. For example,
Input: Original list- [3, 6, 12, 14, 78, 24, 56]
Output: After cloning- [3, 6, 12, 14, 78, 24, 56]
Input: Original list- [14, 7, 9, 13, 46, 12]
Output: After cloning- [14, 7, 9, 13, 46, 12]
For cloning a list in Python we can follow these approaches-
- By following the Slicing technique
- By using extend() method
- By using list() method
- By using list comprehension
- By using append() method
- By using copy() method
We will also compare each approach in terms of their time complexity and find the fastest method.
Approach 1: Slicing Technique
We will use list slicing technique to access and copy the list elements in another list. This is the easiest method to modify any list and make a copy of the list along with the reference. This method takes about 0.039 seconds and is the fastest in cloning a list.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Take input of list from user
Step 2- Declare a new list which will be the copy
Step 3- Copy the elements using slicing operator ( : ) in the new list
Step 4- Print the new list which will be the copy of the original list
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have taken an input of elements of the list and copied the list to a new list by slicing.
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Original list: ",li)
#cloning
list_copy = li[:]
print("After cloning: ",list_copy)
Enter size of list 5
Enter element of list 3
Enter element of list 6
Enter element of list 1
Enter element of list 2
Enter element of list 3
Original list: [3, 6, 1, 2, 3]
After cloning: [3, 6, 1, 2, 3]
Approach 2: extend()
A list can be copied into a new list by using the in-built extend() function. This will add each element of the original list to the end of the new list. This method takes around 0.053 seconds to complete.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Take input of list from user
Step 2- Declare a new list which will be the copy
Step 3- Copy the elements using extend() in the new list
Step 4- Print the new list which will be the copy of the original list
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have input elements of the list from the user and copied it into a new list using the extend() function.
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Original list: ",li)
#cloning
list_copy = []
list_copy.extend(li)
print("After cloning: ",list_copy)
Enter size of list 6
Enter element of list 3
Enter element of list 5
Enter element of list 12
Enter element of list 78
Enter element of list 16
Enter element of list 35
Original list: [3, 5, 12, 78, 16, 35]
After cloning: [3, 5, 12, 78, 16, 35]
Approach 3: list()
We will simply use the list() function to copy the existing list into another list in this approach. This method takes about 0.075 seconds to complete.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Take input of list from user
Step 2- Declare a new list which will be the copy of the original list
Step 3- Initialise the new list and pass the original list in the list() function
Step 4- Print the new list which will be the copy of the original list
Python Program 3
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have to input the list from the user and copy it into a new list by using the list() function.
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Original list: ",li)
#cloning
list_copy = list(li)
print("After cloning: ",list_copy)
Enter size of list 4
Enter element of list 2
Enter element of list 7
Enter element of list 9
Enter element of list 10
Original list: [2, 7, 9, 10]
After cloning: [2, 7, 9, 10]
Approach 4: list comprehension
In this approach, we will use the list comprehension method. List comprehension is a shorter syntax for creating a new list based on the values of an existing list. We can create a copy of the list using list comprehension. The method takes about 0.217 seconds to complete.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Take input of list from user
Step 2- Declare a new list which will be the copy of the original list
Step 3- Use list comprehension to copy the elements in the list
Step 4- All the values in the original list will be stored in the new list
Step 5- Print the new list which will be the copy of the original list
Python Program 4
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have to take input the list from the user and copied it into a new list by using the list comprehension syntax.
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Original list: ",li)
#cloning
list_copy = [ num for num in li ]
print("After cloning: ",list_copy)
Enter size of list 7
Enter element of list 12
Enter element of list 13
Enter element of list 2
Enter element of list 24
Enter element of list 6
Enter element of list 35
Enter element of list 8
Original list: [12, 13, 2, 24, 6, 35, 8]
After cloning: [12, 13, 2, 24, 6, 35, 8]
Approach 5: append()
append() function simply adds the element to the last position of a list. This can be used for copying elements to a new list. This method takes around 0.325 seconds to complete and is the slowest method for cloning a list.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Take input of list from user
Step 2- Declare a new list which will be the copy of the original list
Step 3- Run a loop to access each element in the list
Step 4- Use append() to add each element in the new list one by one
Step 5- Print the new list which will be the copy of the original list
Python Program 5
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have taken an input of elements of the list from the user and added each element of the list one by one to a new list using append().
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Original list: ",li)
#cloning
list_copy = []
for num in li:
list_copy.append(num)
print("After cloning: ",list_copy)
Enter size of list 8
Enter element of list 2
Enter element of list 3
Enter element of list 4
Enter element of list 5
Enter element of list 6
Enter element of list 8
Enter element of list 9
Enter element of list 10
Original list: [2, 3, 4, 5, 6, 8, 9, 10]
After cloning: [2, 3, 4, 5, 6, 8, 9, 10]
Approach 6: copy()
copy() is an in-built method to copy all the elements from one list to another. This method takes around 1.488 seconds to complete and takes the most time out of all the approaches.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Take input of list from user
Step 2- Declare a new list
Step 3- copy the original list to the new list using the copy() function
Step 4- Print the new list
Python Program 6
Look at the program to understand the implementation of the above-mentioned approach. In this program, we have taken an input of elements of the list from the user and added each element of the list one by one to a new list using append().
li=[]
n=int(input("Enter size of list "))
for i in range(0,n):
e=int(input("Enter element of list "))
li.append(e)
print("Original list: ",li)
#cloning
list_copy = li.copy()
print("After cloning: ",list_copy)
Enter size of list 5
Enter element of list 12
Enter element of list 13
Enter element of list 14
Enter element of list 25
Enter element of list 26
Original list: [12, 13, 14, 25, 26]
After cloning: [12, 13, 14, 25, 26]
Conclusion
In this tutorial, we have learned different methods for copying a list in Python. The fastest among them is by using the slicing technique.