While programming in python, we sometimes need to store the same data in multiple places. This may be due to the fact that we need to preserve the original data. In this article, we will discuss different ways to copy a list in python.
Copy a List in Python
When we need to copy an integer in python, we simply assign a variable to another variable as shown below.
num1 = 10
print("The first number is:", num1)
num2 = num1
print("The copied number is:", num2)
Output:
The first number is: 10
The copied number is: 10
Here, we have created a variable num1
with the value 10. Then, we have assigned num1
to another variable num2
. After assignment, even if we change the original variable, the value in the copied variable remains unaffected. You can observe this in the following example.
num1 = 10
print("The first number is:", num1)
num2 = num1
print("The copied number is:", num2)
num1 = 15
print("The first number after modification:", num1)
print("The copied number is:", num2)
Output:
The first number is: 10
The copied number is: 10
The first number after modification: 15
The copied number is: 10
In the above example, you can see that the value in num2
hasn’t been modified after modifying num1
.
Now, let us copy a list using the assignment operation.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
list2 = list1
print("The copied list is:", list2)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The copied list is: [1, 2, 3, 4, 5, 6, 7]
When we change the original list in this case, the copied list also gets modified.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
list2 = list1
print("The copied list is:", list2)
list1.append(23)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list2)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 23]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7, 23]
Why does this happen?
Copy by assignment works for an integer because integers are immutable objects. When an integer is assigned to another integer, both refer to the same object. Once we modify any of the integer variables, a new python object is created and the original python object remains unaffected. You can observe this in the following example.
num1 = 10
print("The id of first number is:", id(num1))
num2 = num1
print("The id of copied number is:", id(num2))
num1 = 15
print("The id of first number after modification:", id(num1))
print("The id of copied number after modification is:", id(num2))
Output:
The id of first number is: 9789248
The id of copied number is: 9789248
The id of first number after modification: 9789408
The id of copied number after modification is: 9789248
Here, you can see that id of both num1
and num2
after copying num1
to num2
using the assignment operator is the same. However, when we modify num1
, the id of num1
changes.
Lists are mutable objects. When we modify a list, the original list object is modified. Hence, no python object is created during the modification of list variables and the change is reflected both in the copied and the original list variable. You can observe this in the following example.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The id of original list is:", id(list1))
list2 = list1
print("The id of copied list is:", id(list2))
list1.append(23)
print("The id of original list after modification is:", id(list1))
print("The id of copied list after modification is:", id(list2))
Output:
The id of original list is: 139879630222784
The id of copied list is: 139879630222784
The id of original list after modification is: 139879630222784
The id of copied list after modification is: 139879630222784
Here, you can see that the id of both lists remains the same even after modification in list1
. Therefore, it is confirmed that the original list and the copied list refer to the same object even after modification.
As the assignment operation doesn’t work for copying lists in python, we will discuss different ways to copy a list in python. Before doing so, let us first discuss the id()
function in python.
The id() Function in Python
Each python object has a unique identifier. We can use the id()
function to obtain the identifier associated with any python object as shown in the above examples.
If two variables, when passed as input to the id()
function, give the same output, both the variables refer to the same object.
If the output of the id()
function is different for different variables, they refer to different objects.
In the upcoming sections, we will use the id()
function to check if a list is copied or not. If a list is successfully copied, the identifiers of both lists will be different. In such a case, when we modify a list, it won’t affect the other list.
If the identifier of both the variables referring to lists is the same, the variables refer to the same list. In this case, any change made in the list associated with a variable will be reflected in the list associated with another variable.
With this background, let us now discuss the different ways to copy a list in python.
Copy a List Using the list() Constructor in Python
The list()
constructor is used to create a new list from any iterable object like a list, tuple, or set. It takes an iterable object as its input argument and returns a new list containing the elements of the input iterable object.
To copy a list using the list()
constructor in python, we will pass the original list as the input argument to the list() constructor.
After execution, the list()
constructor will return a new list containing the elements from the original list. The new list and the original list will have different identifiers that can be obtained using the id()
method. Hence, any change made to one of the lists will not affect the other list. You can observe this in the following example.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list(list1)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 140137673798976
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of copied list is: 140137673851328
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]
In the above example, you can see that the id of the original list and the copied list is different. Therefore, making changes to the original list will not impact the copied list.
Copy a List Using the append() Method in Python
The append()
method is used to add a new element to a list. When invoked on a list, it takes an element as its input argument and adds it to the last position of the list.
To copy a list using the append()
method in python, we will first create an empty list named list_copy
. After that, we will iterate through the original list using a for loop. While iteration, we will add each element of the original list to list_copy
using the append()
method.
After executing the for loop, we will get the copied list in the list_copy
variable. You can observe this in the following example.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for element in list1:
list_copy.append(element)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 140171559405056
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of copied list is: 140171560708032
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]
You can observe that the new list and the original list have different identifiers that we have obtained using the id()
method. Hence, any change made to one of the lists doesn’t the other list.
Copy a List Using the extend() Method in Python
The extend()
method is used to add multiple elements at once to a list. When invoked on a list, the extend()
method takes an iterable object as its input argument. After execution, it appends all the elements of the input iterable object to the list.
To copy a list using the extend()
method, we will first create an empty list named list_copy
. After that, we will invoke the extend()
method on list_copy
with the original list as its input argument. After execution, we will get the copied list in the list_copy
variable.
The new list and the original list will have different identifiers that can be obtained using the id()
method. Hence, any change made to one of the lists will not affect the other list. You can observe this in the following example.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
list_copy.extend(list1)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 139960369243648
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of copied list is: 139960370546624
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]
Copy a List Using Slicing in Python
Slicing in python is used to create a copy of a part of a list. The general syntax for slicing is as follows.
new_list=original_list[start_index:end_index]
Here,
new_list
is the list created after the execution of the statement.original_list
is the given input list.start_index
is the index of the leftmost element that has to be included innew_list
. If we leavestart_index
empty, the default value 0 is taken and the list is copied starting from the first element.end_index
is the index of the rightmost element that has to be included in thenew_list
. If we leaveend_index
empty, the default value is taken to be the length of the list. Hence, the list is copied till the last element.
You can copy a list using slicing as shown in the following example.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1[:]
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 139834922264064
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of copied list is: 139834923567040
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]
You can observe that the new list and the original list have different identifiers that we have obtained using the id()
method. Hence, any change made to one of the lists doesn’t affect the other list.
Copy a List Using List Comprehension in Python
List comprehension is used to create a new list using the elements of an existing list by imposing some conditions on the elements. The general syntax of list comprehension is as follows.
new_list=[element for element in existing_list if condition]
Here,
new_list
is the list created after the execution of the statement.existing_list
is the input list.- The
element
represents elements of the existing list. This variable is used to iterate over theexisting_list
. condition
is a condition imposed on the element.
Here, we need to copy a given list using list comprehension. Hence, we will not use any condition. Following is the code to copy a list in python using the list comprehension in python.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = [element for element in list1]
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 139720431945088
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of copied list is: 139720433248128
You can observe that the new list and the original list have different identifiers that we have obtained using the id()
method. Hence, any change made to one of the lists does not affect the other list.
Copy a List Using the copy() Method in Python
Python also provides us with the copy()
method to copy a list in python. The copy()
method, when invoked on a list, returns a copy of the original list. The new list and the original list will have different identifiers that can be obtained using the id()
method. Hence, any change made to one of the lists will not affect the other list. You can observe this in the following example.
list1 = [1, 2, 3, 4, 5, 6, 7]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1.copy()
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1.append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [1, 2, 3, 4, 5, 6, 7]
The id of original list is: 140450078000576
The copied list is: [1, 2, 3, 4, 5, 6, 7]
The id of copied list is: 140450079303616
The original list after modification is: [1, 2, 3, 4, 5, 6, 7, 10]
The copied list after modification is: [1, 2, 3, 4, 5, 6, 7]
Copy List of Lists in Python
Copying a list of lists in python is not similar to copying a list. For instance, let us copy a list of lists in python using the copy()
method.
list1 = [[1, 2, 3], [4, 5, 6,], [7,8,9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1.copy()
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
Output:
The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139772961010560
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of copied list is: 139772961063040
Here, you can see that id of the original list and the copied list are different. It means that both the lists are different python objects. Despite this, when we perform any modification to the original list, it is reflected in the modified list. You can observe this in the following example.
list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = list1.copy()
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139948423344896
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of copied list is: 139948423397504
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
This situation is unwarranted. It happens due to the storage pattern of the list of lists in the memory. A list of lists contains references to the inner lists.
When we copy a list of lists using the copy()
method, the list of lists along with the references of the inner lists gets copied. Therefore, the inner lists aren’t copied and only the references to the inner lists are copied. Due to this, when we make changes to any of the inner lists in the original list, it is also reflected in the copied list.
To avoid this situation, we can iteratively copy the elements of the list of lists.
Copy List of Lists Using the append() Method in Python
To copy a list of lists using the append()
method in python, we will use the following steps.
- First, we will create an empty list named
list_copy
. - After that, we will iterate over the list of lists using a for loop. For each inner list in the list of lists, we will perform the following tasks.
- First, we will create an empty list named
temp
. After that, we will iterate over the elements of the inner list using another for loop. - While iteration, we will append the elements of the current inner list into
temp
using theappend()
method. Theappend()
method, when invoked ontemp
, will take an element from the inner list as input argument and will append it totemp
.
- First, we will create an empty list named
- After traversing each element of the current inner list, we will append
temp
tolist_copy
. After that, we will move to the next inner list and repeat the previous steps.
Once we iterate over all the inner lists of the input list, we will get the copied list of lists in the copy_list
variable. You can observe this in the following example.
list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for inner_list in list1:
temp = []
for element in inner_list:
temp.append(element)
list_copy.append(temp)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139893771608960
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of copied list is: 139893771661952
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
After iteratively copying the list elements, you can see that modifications in the original list do not affect the copied list.
Copy List of Lists Using the extend() Method in Python
Instead of using the append()
method, we can also use the extend()
method to copy a list of lists in python. For this, we will use the following steps.
- First, we will create an empty list named
list_copy
. - After that, we will iterate over the list of lists using a for loop. For each inner list in the list of lists, we will perform the following tasks.
- First, we will create an empty list named
temp
. After that, we will invoke theextend()
method ontemp
with the inner list as its input argument. - Then, we will append temp to
list_copy
. After that, we will move to the next inner list.
Once we iterate over all the inner lists of the input list, we will get the copied list of lists in the copy_list
variable. You can observe this in the following example.
list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for inner_list in list1:
temp = []
temp.extend(inner_list)
list_copy.append(temp)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 140175921128448
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of copied list is: 140175921181312
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copy List of Lists Using the copy() Method in Python
We can also use the copy()
method to copy a list of lists in python. For this, we will use the following steps.
- First, we will create an empty list named
list_copy
. - After that, we will iterate over the list of lists using a for loop. For each inner list in the list of lists, we will perform the following tasks.
- We will invoke the
copy()
method on the inner list. We will assign the output of thecopy()
method to a variabletemp
. - Then, we will append temp to
list_copy
. After that, we will move to the next inner list.
Once we iterate over all the inner lists of the input list, we will get the copied list of lists in the copy_list
variable. You can observe this in the following example.
list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = []
for inner_list in list1:
temp = inner_list.copy()
list_copy.append(temp)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 140468123341760
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of copied list is: 140468123394560
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copy List of Lists Using the copy Module in Python
The copy
module provides us with the deepcopy()
method with which we can copy nested objects. The deepcopy()
method takes a python object as an input argument and recursively copies all the elements of the input object.
We can copy a list of lists in python using the deepcopy()
method as shown in the following example.
import copy
list1 = [[1, 2, 3], [4, 5, 6, ], [7, 8, 9]]
print("The original list is:", list1)
print("The id of original list is:", id(list1))
list_copy = copy.deepcopy(list1)
print("The copied list is:", list_copy)
print("The id of copied list is:", id(list_copy))
list1[2].append(10)
print("The original list after modification is:", list1)
print("The copied list after modification is:", list_copy)
Output:
The original list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of original list is: 139677987171264
The copied list is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The id of copied list is: 139677987171776
The original list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]
The copied list after modification is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
You can observe that the new list and the original list have different identifiers that we have obtained using the id()
method. Hence, any change made to one list does not affect the other list.
Conclusion
In this article, we have discussed different ways to copy a list in python. We also looked at different ways to copy a list of lists in python.
To learn more about python programming, you can read this article on how to check for sorted list in python. You might also like this article on how to save a dictionary to file in python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.