Python List copy() Method
list.copy() returns a new, shallow copy of the list: a separate list holding the same items, so changing one list does not change the other, but nested objects are still shared.Assigning a list to another name does not copy it: b = a makes both names point at the same list, so changes through one show up in the other. Use copy() when you want an independent list, for instance before sorting or clearing the original.
copy() is equivalent to the slice a[:] and to list(a). How references and copies work in general is covered in copying in Python.
| Syntax | my_list.copy() |
|---|---|
| Parameters | None |
| Returns | A new list with the same items (shallow copy) |
| Time complexity | O(n) |
How list.copy() makes a separate list
The new list is a different object with the same elements in the same order. Adding, removing or reordering items in one list does not affect the other.
a = [1, 2, 3]
b = a.copy()
b.append(4)
print(a)
print(b)
print(a is b)Shallow copy: nested lists are still shared
A shallow copy duplicates only the outer list. The items inside are the same objects. For numbers and strings this does not matter because they cannot be changed in place, but if an item is itself a list, both outer lists hold the same inner list. Changing an inner list through one copy is visible through the other. When you need fully independent nested data, use copy.deepcopy() from the standard library.
copy() vs b = a vs slicing vs list()
| Code | New list object? | Nested items copied? |
|---|---|---|
b = a | No, same object | Not applicable |
a.copy() | Yes | No (shallow) |
a[:] | Yes | No (shallow) |
list(a) | Yes | No (shallow) |
copy.deepcopy(a) | Yes | Yes, recursively |
Examples you can run
Press Run, then edit the code and run it again.
Assignment is not a copy
Both names refer to one list, while copy() gives you a separate one.
a = [1, 2, 3]
same = a
copy = a.copy()
a.append(99)
print(same)
print(copy)Expected output[1, 2, 3, 99]
[1, 2, 3]
Keep the original order while sorting
Copy first, then sort the copy.
scores = [88, 72, 95]
ranked = scores.copy()
ranked.sort(reverse=True)
print(scores)
print(ranked)Expected output[88, 72, 95]
[95, 88, 72]
Shallow vs deep copy of a nested list
The inner list is shared by copy() but not by deepcopy().
import copy
grid = [[1, 2], [3, 4]]
shallow = grid.copy()
deep = copy.deepcopy(grid)
grid[0].append(99)
print(shallow)
print(deep)Expected output[[1, 2, 99], [3, 4]]
[[1, 2], [3, 4]]
Common mistakes
Thinking b = a makes a copy
✗ Wrong
a = [1, 2]
b = a
b.append(3)
print(a)✓ Right
a = [1, 2]
b = a.copy()
b.append(3)
print(a)Use b = a.copy() to get an independent list.
Expecting copy() to duplicate inner lists
✗ Wrong
rows = [[0, 0], [0, 0]]
backup = rows.copy()
rows[0][0] = 5
print(backup)✓ Right
import copy
rows = [[0, 0], [0, 0]]
backup = copy.deepcopy(rows)
rows[0][0] = 5
print(backup)Use copy.deepcopy(rows) for nested data.
Using the return value of the wrong method
✗ Wrong
a = [3, 1, 2]
b = a.copy().sort()
print(b)✓ Right
a = [3, 1, 2]
b = sorted(a)
print(b)sort() returns None. Copy on one line, sort on the next, or use sorted(a).
Python List copy() Method: questions people ask
How do I copy a list in Python?
Use new = my_list.copy(), new = my_list[:] or new = list(my_list). All three create a shallow copy.
What is the difference between a shallow copy and a deep copy?
A shallow copy duplicates only the outer list and shares the inner objects, while copy.deepcopy() duplicates the nested objects too.
Is list.copy() the same as slicing [:]?
Yes, both produce a new shallow copy. copy() is just a more readable way to say it.
Keep learning
Step-by-step lessons:
Copying Lists and DictsPython ListsPython Slicing
Related:
Python List clear() MethodPython List sort() MethodPython List extend() MethodPython List Methods
More in list methods:
Python List append() MethodPython List extend() MethodPython List insert() MethodPython List remove() MethodPython List pop() MethodPython List clear() MethodPython List index() MethodPython List count() Method See all →