Python 3 reference · List Methods

Python List copy() Method

Return a shallow copy of the list: a new list with the same items. Nested objects are still shared.

Python 3 Since 3.0

Syntax

list.copy()

Return value

A new list.

Examples

Copy before changingTry it yourself
original = [1, 2, 3]
clone = original.copy()
clone.append(4)
print(original, clone)

Output

[1, 2, 3] [1, 2, 3, 4]
Shallow vs deep copiesTry it yourself
import copy
grid = [[0, 0], [0, 0]]
shallow = grid.copy()
shallow[0][0] = 9
print(grid)  # the inner list is shared
deep = copy.deepcopy(grid)
deep[1][1] = 7
print(grid, deep)

Output

[[9, 0], [0, 0]]
[[9, 0], [0, 0]] [[9, 0], [0, 7]]

items.copy(), items[:] and list(items) all make shallow copies. Use copy.deepcopy() for nested lists.

Version history

VersionChange
Python 3.0Added