Copy List Python Without Reference
Last Updated :
08 Feb, 2024
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
Below are the ways to copy list without referencing the original list in Python.
Using
Slicing
Using 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
original_list = [ 1 , 2 , 3 , 4 , 5 ]
copied_list = original_list[:]
print ( "Original List:" , original_list)
print ( "Copied List:" , copied_list)
|
Output
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
original_list = [ 1 , 2 , 3 , 4 , 5 ]
copied_list = list (original_list)
print ( "Original List:" , original_list)
print ( "Copied List:" , copied_list)
|
Output
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
import copy
original_list = [ 1 , 2 , 3 , 4 , 5 ]
copied_list = copy.copy(original_list)
print ( "Original List:" , original_list)
print ( "Copied List:" , copied_list)
|
Output
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
import copy
original_list = [[ 1 , 2 ], [ 3 , 4 ]]
copied_list = copy.deepcopy(original_list)
print ( "Original List:" , original_list)
print ( "Copied List:" , copied_list)
|
Output
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.
Share your thoughts in the comments
Please Login to comment...