Python Copy a List
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().
Example
Make a copy of a list with the copy() 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 method list().
Example
Make a copy of a list with the list() method:
thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
Try it Yourself »
mylist = list(thislist)
print(mylist)
Related Pages
Python Lists Tutorial Lists Access List Items Change List Item Loop List Items List Comprehension Check If List Item Exists List Length Add List Items Remove List Items Join Two Lists