Python Glossary Русский

Python 3.3+Lists (list)function

list.copy

copying

Return a shallow copy: a new list with the same element references; nested objects are not copied.

Syntax

lst.copy()

The copy is shallow: the outer list is new, but the elements inside are the very same objects. Mutate a nested list or dict through the copy and the original changes too — reach for copy.deepcopy() when you need real independence. lst[:] and list(lst) do exactly the same job, while b = a copies nothing at all: it is just a second name for one list.

Examples

a = [1, 2, 3]
b = a.copy()
b.append(4)
print(a)   # → [1, 2, 3]
print(b)   # → [1, 2, 3, 4]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy