In some operations in Python, you may need to copy the contents of a list. In this article, we will look at the different ways of copying or cloning the contents of a list.
How to copy lists in Python
To copy or clone a list in Python, the following methods can be used:
- = operator method
- copy() method
- slice() method
- list() method
- deepcopy() method
- copy.copy() shallow copy method
- comprehension method
- extend() method
1.Copy list using = operator
Example:
# Python program for list copy
my_list = [1, 2, 3, 4, 5]
copy_list = my_list
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [1, 2, 3]
Execution Time: 0.0019 Second
Using this code, the contents of the my_list will be transferred over to the copy_list. But there is a problem using this method. In case you have to modify the original list contents, the new list will also be modified.
So if you want to keep the original list unchanged, you have to use the in-built method copy (). You do not need to use any parameters for this method. This method returns a new list.
2.Copy list using copy() method
Sometimes, you may want to work on a list but do not want to modify its contents. The best way to go about this is by creating a clone or copy of the original list. For this, Python has an inbuilt method called copy (). This method makes a copy of the original list into a new list. You can then work on this cloned list without affecting the contents of the initial list. Thus, any modification of the new list will not be reflected on the original list.
Example:
# Python program for list copy
# Using copy() method
# Declare list
my_list = [1, 2, 3, 4, 5]
# Copy list using copy method
copy_list = my_list.copy()
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [1, 2, 3, 4, 5]
Execution Time: 0.0009 Second
3.Copy list using the List slice method
Another method for cloning the elements of a list is by using the slicing technique. You can achieve this using the slice() method. You can use this method when you want to change a list, but also keep a copy of the list. Using this method, you will be able to fetch certain contents of the list.
The slice() function returns a slice object that is represented by a specific range. You can use this method to slice any sequence. A sequence can be a list, tuple, string, byte or a range. The slice () method takes three parameters to work.
The parameters are as follows:
- start – This is an optional parameter. It specifies the starting position of the slice. The default integer is 0.
- end – This is the integer that specifies where the slicing ends.
- Step – This is another optional parameter. It specifies the increment between each index of the slice. The default integer is 1.
- Syntax: obj[start:stop:step]
If you do not mention the start or end parameter, they are set to none. The technique is considered the fastest for copying list items.
Example:
import time
# Python program for list copy
# Using slice() method
# declare list
my_list = [1, 2, 3, 4, 5]
# Copy list using slice() method
copy_list = my_list[:]
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [1, 2, 3, 4, 5]
Execution Time: 0.0000 Second
4.Copy list using list() method
The list() method is considered the simplest method of cloning list items. This function creates a list object. Any list object is a collection of elements that have a specific order. The built-in function takes only one parameter called iterable. This can be a sequence such as a tuple or string. It can also be a collection or an iterator object.
The list() method is a constructor that returns a sequence of list elements that you can modify. But there are two types of return values:
- In case you do not pass any parameter for the method, an empty list will be created
- If you pass an iterable parameter to the function, it creates a list from the iterable parameter
Let us look at an example:
# Python program for list copy
# Using list() method
# Declare list
my_list = [1, 2, 3, 4, 5]
# Copy list using list method
copy_list = list(my_list)
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [1, 2, 3, 4, 5]
Execution Time: 0.0006 Second
5.Copy list using the deep copy method
A deep copy is a method through which the elements of a list are copied recursively. In this process, a collection object is created at first. This collection object is populated by the copies of nested objects in the initial list. Here, the copy of one object is copied to another object. So, if you make any changes to the copy of the object, it will not affect the original object.
You can perform a deep copy with the deepcopy() function. This is the slowest method of cloning elements.
An example of using the deepcopy () method is:
import copy
# Declare recursive list
my_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
# Copy list using deepcopy() method
copy_list = copy.deepcopy(my_list)
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Execution Time: 0.0029 Second
Here, if you make any modifications in the nested objects of the my_list, there will be no changes in the copy_list.
6.Copy list using the shallow copy method
The shallow copy method creates a new collection object. The method then populates the object with references to the original elements. The process is not recursive, and no copies of the nested objects are created. Here, a reference of one object is copied into another. Thus changes in the copy will not affect the original.
Let us look at an example:
# Python program for list copy
# Using shallow copy method
import copy
# Declare recursive list
my_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
# Copy list using copy() method
copy_list = copy.copy(my_list)
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Execution Time: 0.003989219665527344
7.Copy list using list comprehension method
This method is used for copying items of a list to another list. In this method, a new list is created from another list or a string. This resultant list is created using a for statement and conditional statements.
Example:
my_list = []
for letter in 'Stechies':
my_list.append(letter)
print('New List: ', my_list)
Output:
Execution Time: 0.0 Second
In this program, the letters of the word ‘Stechies’ are separated in the for a statement. These letters are then inserted as separate items in the new list called my_list.
8.Copy list using extend() method
In the extend method, the elements of one list are copied into another list by adding them at the end. The method takes in a single parameter that is a list. The elements in this argument are added to the original list. Elements of native data types such as a set or a tuple can also be passed as arguments.
Example:
# Python program for list copy
# Using extend() method
# Declare recursive list
my_list = [1, 2, 3, 4, 5]
# Declare empty final list
copy_list = []
# Copy list using extend() method
copy_list.extend(my_list)
print('Old List: ', my_list)
print('New List: ', copy_list)
Output:
New List: [1, 2, 3, 4, 5]
Execution Time: 0.0017018318176269531
Best and worst methods
The fastest list cloning technique is the list slicing method that uses the slice operator “:”. Here the original contents can be secured. The second fastest list copying method is extend() that takes a list and adds it at the end of another list. Another simple and fast method is the list() method.
The deepcopy() method is the slowest as it takes 10.59 seconds to execute. This is because it is a recursive method. The in-built copy() method is the next most gradual process of cloning elements.
Conclusion
You can use any of the methods for copying list items. The choice of method depends upon the execution time that is convenient for you. The size of the list is also essential. For example, there may be a list containing many elements. Using a method such as deepcopy() will be very time-consuming. Similarly, for shorter lists, you can use the slicing technique and extend() method.