Loading lesson path
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a to list1, and changes made in list1 will automatically also be made in list2.
Formula
You can use the built - in List method copy()to copy a list.
Make a copy of a list with the copy() method: thislist = ["apple", "banana", "cherry"] mylist = thislist.copy()
print(mylist)Formula
Another way to make a copy is to use the built - in method list().Make a copy of a list with the list() method: thislist = ["apple", "banana", "cherry"] mylist = list(thislist)
print(mylist)You can also make a copy of a list by using the
(slice) operator.
Make a copy of a list with the
operator: thislist = ["apple", "banana", "cherry"]
Formula
mylist = thislist[:]print(mylist)