List copy (copy(), slicing, list())
Creates a shallow copy of a list to avoid mutating the original.
b = a.copy()
b = a[:]
b = list(a)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Creates a shallow copy of a list to avoid mutating the original.
In Python, copying a list using copy(), slicing [:], or the list() constructor creates a shallow copy. This means a new list object is initialized, but the elements inside remain references to the original objects. For immutable elements like integers or strings, this is safe. However, for mutable nested objects (like lists within lists), changes to the inner objects will reflect in both the original and the copy. Performance-wise, slicing (a[:]) is often the fastest method in CPython, while list.copy() is preferred for readability (introduced in Python 3.3). For truly independent copies of nested structures, the copy.deepcopy() function from the standard library is required to recursively copy all objects.
Quick reference
Syntax
b = a.copy()
b = a[:]
b = list(a)
See it in practice
Examples
Shallow Copy using Slicing
original = [10, 20, 30]
shallow_copy = original[:]
shallow_copy.append(40)
print(f"Original: {original}")
print(f"Copy: {shallow_copy}")Original: [10, 20, 30] Copy: [10, 20, 30, 40]
Using the [:] slice syntax creates a new list containing the same references as the original. Adding an item to the copy does not affect the original list.
Copying with the list() Constructor
items = ['apple', 'banana']
new_list = list(items)
new_list[0] = 'cherry'
print(items)
print(new_list)['apple', 'banana'] ['cherry', 'banana']
The list() constructor is a common way to create a shallow copy while explicitly casting the type, ensuring the new object is a list even if the source was another iterable.
The Shallow Copy Pitfall (Nested Lists)
matrix = [[1, 2], [3, 4]]
dup = matrix.copy()
dup[0].append(99)
print(f"Matrix: {matrix}")
print(f"Duplicate: {dup}")Matrix: [[1, 2, 99], [3, 4]] Duplicate: [[1, 2, 99], [3, 4]]
Since copy() is shallow, the nested lists are shared between 'matrix' and 'dup'. Modifying a nested list affects both parent lists.
Debug faster
Common Errors
LogicError
Cause: Attempting to copy a list by simple assignment (b = a), which only creates a new reference to the same object.
Fix: Use a proper copy method like .copy() or [:] to ensure changes to the new variable do not mutate the original.
a = [1, 2]
b = a
b.append(3) # a is now [1, 2, 3]AttributeError
Cause: Using the .copy() method in Python 2.x environments, where it was not available for lists.
Fix: Use slicing [:] or list() which are compatible across Python 2 and 3, or upgrade to Python 3.3+.
my_list = [1, 2]
# This fails in Python 2.7
new_list = my_list.copy()Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Creates a shallow copy of a list to avoid mutating the original.
LogicError: Use a proper copy method like .copy() or [:] to ensure changes to the new variable do not mutate the original. AttributeError: Use slicing [:] or list() which are compatible across Python 2 and 3, or upgrade to Python 3.3+.