9.0 Python: Copy Lists
9.1 Copy a List
Warning
You cannot copy a list simply by typing list2 = list1
, because: list2
will only be a reference to list1
, and changes made in list1
will automatically also be made in list2
.
9.2 Use the copy()
method
You can use the built-in List method copy()
to copy a list.
9.2.1 Example
Make a copy of a list with the copy()
method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
9.3 Use the list()
method
Another way to make a copy is to use the built-in method list()
.
9.3.1 Example
Make a copy of a list with the list()
method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
9.4 Use the slice Operator
You can also make a copy of a list by using the :
(slice) operator.
thislist = ["apple", "banana", "cherry"]
mylist = thislist[:]
print(mylist)