Why Python’s [:] List Copy Is Misleading and Better Alternatives
Python’s slice syntax new = old[:] appears to copy a list, but it actually creates a reference to the same object, leading to subtle bugs; this article explains Python’s object model, demonstrates the pitfalls, and presents clearer alternatives such as list(), copy.copy(), and deepcopy.
Python’s slice syntax new = old[:] copies a list, but many beginners misunderstand it and should avoid this pattern.
In Python, variables are merely labels that reference objects; they are not memory locations as in C. Assigning a = [1, 2, 3] creates a list object and binds the name a to it. A subsequent assignment b = a does not create a new list; it creates another label b that points to the same list object.
Modifying the list through one label affects the other, as shown by appending to a and printing both a and b. The built‑in function id() returns the unique identifier (memory address) of an object, confirming that a and b share the same id.
To obtain an actual copy, a new list must be created and populated with the contents of the original. The slice operator [:] returns a shallow copy of the sequence, creating a new list object that contains references to the same elements.
a[1:3] # returns [2, 3]
id(a) # e.g., 3086056
id(a[1:3]) # different id, e.g., 3063400Omitting the start index ( a[:3]) copies from the beginning, while omitting the end index ( a[1:]) copies to the end. However, slice copying is not the only method.
b = list(a) # creates a new list using the list constructorThe list() constructor is more explicit and Pythonic; it can build a list from any iterable, including tuples and generators, which slice syntax cannot handle.
my_tuple = (1, 2, 3)
my_list = list(my_tuple) # [1, 2, 3]Generators are also convertible:
generator = (x * 3 for x in range(4))
list(generator) # [0, 3, 6, 9]For deeper copying, especially when the list contains nested mutable objects, copy.deepcopy() is required; shallow copies ( a[:], list(a), a*1, copy.copy(a)) duplicate the outer list but keep references to inner objects.
import copy
a = [[10], 20]
b = a[:] # shallow copy
c = list(a) # shallow copy
d = a * 1 # shallow copy
e = copy.copy(a) # shallow copy
f = copy.deepcopy(a) # deep copyPrinting the ids and contents shows that f is the only fully independent copy when the original list contains sub‑lists.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
