Chapters
Python114 chapters

ReferenceList methods

list.copy()

A new list holding the same items. Shallow: nested objects are still shared.

Signature

items.copy()

Returns

A new list.

Example

Python
a = [1, 2]
b = a.copy()
b.append(3)
print(a, b)

grid = [[1]]
shallow = grid.copy()
shallow[0].append(2)
print(grid)

Output

[1, 2] [1, 2, 3]
[[1, 2]]

See also