How to Clone or Copy Python List? (6 Methods)
Using assignment operator, we can not have two different list because both list variables point same memory location. For your verification see example below:
first_list = [1, 2, 3, 4, 5]
second_list = first_list
print("First list address: ", id(first_list))
print("Second list address: ", id(second_list))
Output
First list address: 1942422416840 Second list address: 1942422416840
Here memory address of first_list
and second_list
are same so they are refering the same list object [1, 2, 3, 4, 5]
To actually copy or clone list in Python, we have following methods:
1. Using List Copy Method
We can use copy()
method available in list to clone a list. See example below:
original_list = [1, 2, 3, 4, 5]
# copy
copied_list = original_list.copy()
print("Original list: ", original_list)
print("Copied list: ", copied_list)
print("Original list address: ", id(original_list))
print("Copied list address: ", id(copied_list))
Output
Original list: [1, 2, 3, 4, 5] Copied list: [1, 2, 3, 4, 5] Original list address: 1942422549128 Copied list address: 1942422549000
2. Using Slice Notation
This works because slices of Python lists create new objects in memory. See example below:
original_list = [1, 2, 3, 4, 5]
# copy
copied_list = original_list[:]
print("Original list: ", original_list)
print("Copied list: ", copied_list)
print("Original list address: ", id(original_list))
print("Copied list address: ", id(copied_list))
Output
Original list: [1, 2, 3, 4, 5] Copied list: [1, 2, 3, 4, 5] Original list address: 1942422523720 Copied list address: 1942422549256
3. Using Built-in list() Function
We can use built-in list()
function to create new copy of list. See example below:
original_list = [1, 2, 3, 4, 5]
# copy
copied_list = list(original_list)
print("Original list: ", original_list)
print("Copied list: ", copied_list)
print("Original list address: ", id(original_list))
print("Copied list address: ", id(copied_list))
Output
Original list: [1, 2, 3, 4, 5] Copied list: [1, 2, 3, 4, 5] Original list address: 1942422490696 Copied list address: 1942422748040
4. Using Generic copy() method from copy Module
We can use copy()
method from copy
module to make a brand new copy of list as:
from copy import copy
original_list = [1, 2, 3, 4, 5]
# copy
copied_list = copy(original_list)
print("Original list: ", original_list)
print("Copied list: ", copied_list)
print("Original list address: ", id(original_list))
print("Copied list address: ", id(copied_list))
Output
Original list: [1, 2, 3, 4, 5] Copied list: [1, 2, 3, 4, 5] Original list address: 1942422520328 Copied list address: 1942422488392
5. Using Generic deepcopy() from copy Module
If the list contains objects and you want to copy these object as well then you can use generic deepcopy()
method from copy
module. See example below:
from copy import deepcopy
original_list = [1, 2, 3, 4, 5]
# copy
copied_list = deepcopy(original_list)
print("Original list: ", original_list)
print("Copied list: ", copied_list)
print("Original list address: ", id(original_list))
print("Copied list address: ", id(copied_list))
Output
Original list: [1, 2, 3, 4, 5] Copied list: [1, 2, 3, 4, 5] Original list address: 1942422490504 Copied list address: 1942422489608
6. Using [ ] and *
You can make copy of new list using [ ]
and *
operators. This is same as making copy using list()
method. See example below:
from copy import deepcopy
original_list = [1, 2, 3, 4, 5]
# copy
copied_list = [*original_list]
print("Original list: ", original_list)
print("Copied list: ", copied_list)
print("Original list address: ", id(original_list))
print("Copied list address: ", id(copied_list))
Output
Original list: [1, 2, 3, 4, 5] Copied list: [1, 2, 3, 4, 5] Original list address: 1942422488840 Copied list address: 1942422488712