PythonCopy a List
Copy a List
You cannot copy a list simply by typinglist2 = list1, because:list2 will only be areference tolist1, and changes made inlist1 will automatically also be made inlist2.
There are ways to make a copy, one way is to use the built-in List methodcopy().
Example
Make a copy of a list with thecopy() method:
thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)
Try it Yourself »mylist = thislist.copy()
print(mylist)
Another way to make a copy is to use the built-in methodlist().
Example
Make a copy of a list with thelist() method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
Try it Yourself »mylist = list(thislist)
print(mylist)
Related Pages
Python Lists TutorialListsAccess List ItemsChange List ItemLoop List ItemsList ComprehensionCheck If List Item ExistsList LengthAdd List ItemsRemove List ItemsJoin Two Lists

