Fundamentals 8 min read

Why Using = in Python Can Delete Your Data: Common Copy Pitfalls Explained

The article reveals how the Python assignment operator creates references instead of copies, leading to accidental data loss, and walks through safe copying techniques (.copy(), list(), [:]) with concrete examples, performance benchmarks, and guidance for nested structures.

Data STUDIO
Data STUDIO
Data STUDIO
Why Using = in Python Can Delete Your Data: Common Copy Pitfalls Explained

Bug reproduction: shared list mutation

Assigning one list variable to another with backup_destinations = initial_destinations creates a second name for the same list object. Removing an element via the second name also removes it from the original list.

# Original list
initial_destinations = ["巴厘岛", "龙目岛", "松巴哇岛"]
backup_destinations = initial_destinations
backup_destinations.remove("巴厘岛")
print(initial_destinations)  # [] – Bali vanished

Why the assignment is not a copy

In Python the equal sign binds a name to an existing object; it does not duplicate the object's contents. Both names refer to the same underlying list, so any in‑place mutation (e.g., remove()) affects the shared object.

Three safe shallow‑copy techniques

Method 1: .copy()

initial_destinations = ["巴厘岛", "龙目岛", "松巴哇岛"]
backup_destinations = initial_destinations.copy()  # true copy
backup_destinations.remove("巴厘岛")
print(initial_destinations)  # ['巴厘岛', '龙目岛', '松巴哇岛']
print(backup_destinations)   # ['龙目岛', '松巴哇岛']

Method 2: list() constructor

kitchen_groceries = ["糖", "咖啡", "茶"]
monthly_groceries = list(kitchen_groceries)  # new container
monthly_groceries.remove("糖")
print(kitchen_groceries)  # ['糖', '咖啡', '茶']
print(monthly_groceries)   # ['咖啡', '茶']

Method 3: Full slice [:]

high_scores = [100, 98, 95]
final_scores = high_scores[:]  # shallow copy
final_scores.remove(95)
print(high_scores)  # [100, 98, 95]
print(final_scores) # [100, 98]

Performance comparison (CSDN benchmark, Python 3.10+, 10 million copy operations)

Direct assignment (reference) : 0.0001 s, very low memory (fastest but shares data)

Slice [:] : 0.012 s, medium memory

list() constructor : 0.013 s, medium memory

copy.copy() : 0.015 s, medium memory

List comprehension : 0.028 s, medium memory

copy.deepcopy() : 0.24 s, very high memory (deep copy)

Shallow copy limitation with nested structures

All three methods above produce shallow copies: only the outer list is duplicated. Inner mutable objects remain shared.

import copy
original = [[1, 2], [3, 4]]
shallow_copy = original[:]          # shallow copy
deep_copy = copy.deepcopy(original) # deep copy
shallow_copy[0][0] = 999
print(original)   # [[999, 2], [3, 4]] – mutated!
print(deep_copy)   # [[1, 2], [3, 4]] – untouched

Practical checklist

When you see = list_a, remember it only creates a new label; use a copy if you need independent data.

For single‑level lists, .copy() or [:] are concise and performant.

For nested or multi‑dimensional data (e.g., JSON processing), use copy.deepcopy() to avoid hidden sharing.

Key takeaways

Reference semantics : = binds a name to an existing object; it does not copy.

Safe shallow copies : .copy(), list(), and [:] each create an independent outer list.

Deep copy when needed : copy.deepcopy() is the only method that recursively duplicates nested mutable objects.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonPerformance Benchmarkdeep copyassignmentshallow copyreference semanticslist copy
Data STUDIO
Written by

Data STUDIO

Click to receive the "Python Study Handbook"; reply "benefit" in the chat to get it. Data STUDIO focuses on original data science articles, centered on Python, covering machine learning, data analysis, visualization, MySQL and other practical knowledge and project case studies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.