Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python List Copy vs Slicing: What Actually Happens

Understand how Python list copy() and slicing both create shallow copies, when nested list data is shared, and how to choose the right approach.

~4 min readPython
#python#list-copy#slicing#shallow-copy
Diagram showing two Python list objects sharing references to the same nested elements after a shallow copy operation.

When you write new_list = old_list[:] or new_list = old_list.copy(), both create a new list object. The common question is whether these operations behave identically in every situation. In modern Python, the answer is yes: both perform a shallow copy, creating a new list that holds references to the same elements. The choice between copy() and slicing is mostly stylistic, with one compatibility caveat: list.copy() was added in Python 3.3, while slicing works on older interpreters too. More important than that choice is understanding shallow-copy behavior, especially for nested lists, and knowing when you need copy.deepcopy().

What Slicing Actually Returns

Slicing with [:] returns a new list containing references to the same elements. The slice operator allocates a new list object and copies the references from the original list into it. This is why old_list[:] is old_list evaluates to False while old_list[:] == old_list evaluates to True.

python
original = [1, 2, 3] sliced = original[:] print(sliced is original) # False print(sliced == original) # True

The is comparison checks object identity, while == checks value equality. Both copy() and slicing produce a distinct list object with the same contents. The identity difference is the entire point of copying: you get a separate object you can mutate without affecting the original.

Shallow Copy Semantics

Both copy() and slicing perform a shallow copy. The new list holds references to the same objects as the original list. For immutable elements like integers and strings, this distinction rarely matters because those objects cannot be modified in place.

python
numbers = [1, 2, 3] copy_one = numbers.copy() copy_two = numbers[:] copy_one.append(4) print(numbers) # [1, 2, 3] print(copy_one) # [1, 2, 3, 4]

Appending to the copy does not affect the original because the list object itself is distinct. The confusion arises when the list contains mutable objects, because those objects are shared between the original and the copy.

Nested Lists and the Shallow Copy Trap

Consider a list that contains another list:

python
matrix = [[1, 2], [3, 4]] shallow = matrix[:] shallow[0].append(99) print(matrix) # [[1, 2, 99], [3, 4]]

The inner list [1, 2] is shared between matrix and shallow. Modifying it through either reference changes the data visible through both. This is not a bug in slicing or copy(); it is the defined behavior of shallow copy. If you need independent nested lists, you must use copy.deepcopy().

python
import copy matrix = [[1, 2], [3, 4]] deep = copy.deepcopy(matrix) deep[0].append(99) print(matrix) # [[1, 2], [3, 4]] print(deep) # [[1, 2, 99], [3, 4]]

deepcopy recursively copies the object graph, so the new structure no longer shares mutable objects with the original. That independence comes at the cost of additional time and memory.

Performance and Memory Characteristics

Slicing and copy() have the same runtime complexity: O(n), where n is the number of elements in the list. Both allocate a new list and copy n references. There is no meaningful performance difference between the two for typical workloads.

The more important performance consideration is the difference between shallow and deep copying. A shallow copy of a large list is fast because it only copies references. A deep copy must traverse the entire object graph, which can be substantially slower for deeply nested structures.

The cost difference comes from the number of objects created. A shallow copy creates one new list. A deep copy creates a new list plus a new object for every distinct mutable object reachable from the original. For a list of 10,000 distinct inner lists, a deep copy creates 10,001 new list objects, while a shallow copy creates only one. This allocation overhead is the dominant factor, not the copy operation itself.

Choosing Between copy(), Slicing, and Alternatives

The decision between copy() and slicing is largely stylistic. Both are shallow copies with identical behavior. Python's copy() method was introduced in Python 3.3, so slicing is the more backward-compatible choice for code that must run on older interpreters.

The list() constructor is a third option that also performs a shallow copy:

python
original = [1, 2, 3] constructed = list(original)

All three approaches produce a new list with the same elements. The list() constructor is sometimes preferred when the source is not guaranteed to be a list, since it accepts any iterable. For nested structures, the choice is not between copy() and slicing but between shallow and deep copying. Use copy.deepcopy() only when you need independent copies of nested mutable objects, and be aware of the cost.

When Slicing Is Not a Full Copy

Slicing with a start and end index does not always copy the entire list. A slice like data[1:3] returns a new list containing only the elements at indices 1 and 2. This is still a new list object, but it contains a subset of the references.

python
data = [10, 20, 30, 40] subset = data[1:3] print(subset) # [20, 30]

The full slice [:] is a special case that copies every element. Understanding this distinction helps avoid confusion when reading code that uses partial slices for extraction rather than duplication.

A Note on Memory Sharing with Slices

A common misconception is that slicing creates a view into the original list, similar to how NumPy arrays behave. Python lists do not support views. Every slice creates a new list object with copied references. If you need view-like behavior, you must use a different data structure, such as a NumPy array. The standard library array module is not a view implementation: slicing an array.array returns a new array, not a view.

python
import array arr = array.array('i', [1, 2, 3, 4]) slice_copy = arr[1:3] slice_copy[0] = 99 print(arr) # array('i', [1, 2, 3, 4]) print(slice_copy) # array('i', [99, 3])

This distinction matters when moving between list and array code: a slice of a built-in list, or of a standard-library array, never affects the source, while NumPy slicing commonly returns a view.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.