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 = ["apple", "banana", "cherry"]
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 = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4