Copy List Python Without Reference
In Python, It is common knowledge that when we assign a list to another variable, we are creating a reference to the original list. But how to create a copy of the list without referencing the original. In this article, we will study different approaches by which we can create a copy of the list without referencing the original list.
Copy List Without Referencing Original List in Python
Using
SlicingUsing list()
Constructor- Using Shallow Copy
- Using
copy
module –deepcopy()
Copy List Without Referencing Original List Using Slicing
In this approach, we make a copy of the list without 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 Copy List Without Reference # using Slice Operator original_list = [ 1 , 2 , 3 , 4 , 5 ] copied_list = original_list[:] # Driver Code print ( "Original List:" , original_list) print ( "Copied List:" , copied_list) |
Original List: [1, 2, 3, 4, 5] Copied List: [1, 2, 3, 4, 5]
Copy List Without Referencing Original List Using list()
Constructor
In this approach we use list()
constructor to creates a new list by iterating over the elements of the original list.
Python3
# Using list() Constructor original_list = [ 1 , 2 , 3 , 4 , 5 ] copied_list = list (original_list) # Driver Code print ( "Original List:" , original_list) print ( "Copied List:" , copied_list) |
Original List: [1, 2, 3, 4, 5] Copied List: [1, 2, 3, 4, 5]
Copy List Without Referencing Original List Using copy
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 # using copy() from the copy module original_list = [ 1 , 2 , 3 , 4 , 5 ] # using copy for shallow copy copied_list = copy.copy(original_list) # Output print ( "Original List:" , original_list) print ( "Copied List:" , copied_list) |
Original List: [1, 2, 3, 4, 5] Copied List: [1, 2, 3, 4, 5]
Copy List Without Referencing Original List Using copy
module – deepcopy()
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 # Using deepcopy() from the copy module original_list = [[ 1 , 2 ], [ 3 , 4 ]] copied_list = copy.deepcopy(original_list) # Driver code print ( "Original List:" , original_list) print ( "Copied List:" , copied_list) |
Original List: [[1, 2], [3, 4]] Copied List: [[1, 2], [3, 4]]
Conclusion
In this article we studied different approaches on how to create a copy of a list in Python without referencing the original list. We also understood that it is important because it helps in to prevent unintended side effects when modifying the copied list.The choice of method depends on the nature of the data and the desired outcome. It is essential to consider whether a shallow or deep copy is needed, especially when dealing with nested structures. Understanding these methods empowers developers to choose the most appropriate solution based on the specific requirements of their code.
Contact Us