EN
Python - copy list
0
points
In this article, we would like to show you how to copy list in Python.
Quick solution:
my_list = ['A', 'B', 'C']
list_copy = my_list.copy()
print(list_copy) # ['A', 'B', 'C']
1. Practical example using copy()
method
In this example, we use copy()
method to create a copy of my_list
.
my_list = ['A', 'B', 'C']
list_copy = my_list.copy()
print(list_copy) # ['A', 'B', 'C']
Output:
['A', 'B', 'C']
Note:
You can't copy the list by simply using
list2 = list1
becauselist2
will only be a reference tolist1
.
2. Using list()
constructor
In this example, we use list()
constructor to create a copy of my_list
.
my_list = ['A', 'B', 'C']
list_copy = list(my_list)
print(list_copy) # ['A', 'B', 'C']
Output:
['A', 'B', 'C']