copy
list.copy
Returns a shallow copy of the list — a new list containing the same element references. Equivalent to lst[:] or list(lst); does not recursively copy nested mutables.
list.copy() Parameters
| Parameter | Purpose |
|---|---|
| (no args) | copy() takes no arguments. |
| shallow only | For nested lists/dicts, use copy.deepcopy() to duplicate the inner objects too. |
Examples
>>> a = [1, 2, 3]
>>> b = a.copy()
>>> b.append(4)
>>> a
[1, 2, 3] Independent top-level lists.
>>> a = [[1, 2], [3]]
>>> b = a.copy()
>>> b[0].append(99)
>>> a
[[1, 2, 99], [3]] Shallow — the inner list is shared between a and b.
>>> import copy
>>> b = copy.deepcopy(a) deepcopy fully clones nested structures.
>>> a = [1, 2]; b = a[:]
>>> b is a
False lst[:] and list(lst) are the classical shallow-copy idioms.
Gotcha
It is a SHALLOW copy — nested mutables (lists, dicts, objects) are still shared with the original. Use copy.deepcopy for a fully independent tree.
Related methods
list[i] / list[i:j:k]
Subscript syntax to read a single element (by index) or a new list (by slice). Slicing always returns a new shallow copy; out-of-range slice bounds are silently clamped while single-index access raises IndexError.
list.extend
Extends the list by appending every element from an iterable, in order. Mutates in place and returns None; equivalent to `list += iterable`.
sorted (built-in)
Built-in function that returns a NEW sorted list from any iterable, leaving the source untouched. Uses the same stable Timsort and key/reverse arguments as list.sort.
← All Python list methods · Dict methods · list vs tuple vs set