Python - Copy Lists
Copy a List
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
.
There are ways to make a copy, one way is to use the built-in List method copy()
.
例子
Make a copy of a list with the copy()
method:
thislist = [“苹果”, “香蕉”, “樱桃”]
mylist = thislist.copy()
print(mylist)
亲自尝试 »
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 = [“苹果”, “香蕉”, “樱桃”]
mylist = list(thislist)
print(mylist)
亲自尝试 »