ReferenceList methods

Python List copy() Method

Short answer
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.

Quick facts
Syntaxmy_list.copy()
ParametersNone
ReturnsA new list with the same items (shallow copy)
Time complexityO(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.

Python
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()

CodeNew list object?Nested items copied?
b = aNo, same objectNot applicable
a.copy()YesNo (shallow)
a[:]YesNo (shallow)
list(a)YesNo (shallow)
copy.deepcopy(a)YesYes, 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.

Assignment is not a copy
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.

Keep the original order while sorting
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().

Shallow vs deep copy of a nested list
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)
[1, 2, 3] (a changed too)

✓ 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)
[[5, 0], [0, 0]] (backup changed)

✓ 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)
None

✓ 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 →