Learning Tips | Published Nov 15, 2026 | 7 min read

Python Shallow Copy vs Deep Copy

Learn how Python assignment, shallow copies and deepcopy behave with flat lists and nested dictionaries through practical examples.

Python tutorial comparing shallow and deep copies of nested lists

Assigning a Python list to a second variable does not make an independent copy. This practical guide explains references, shallow copies and deep copies, with small examples to help you choose the right approach for nested data.

Assignment creates another reference

In Python, an assignment binds a name to an object. It does not copy that object. When two names refer to the same mutable list, a change through either name is visible through both:

scores = [70, 82, 91]
backup = scores

backup.append(88)
print(scores)  # [70, 82, 91, 88]
print(scores is backup)  # True

This is useful when two names should point to the same data. If you want to modify one list independently, make a copy.

Make a shallow copy of a flat list

A list's .copy() method, a full slice, or copy.copy() creates a new outer list. For a flat list of immutable values such as integers or strings, this usually gives the independent list you expect:

scores = [70, 82, 91]
draft = scores.copy()

draft.append(88)
print(scores)  # [70, 82, 91]
print(draft)   # [70, 82, 91, 88]

The outer lists are different objects. A shallow copy does not recursively duplicate objects stored inside the list.

Why a nested list behaves differently

Consider a list of learner records, where each record is itself a mutable dictionary:

import copy

learners = [
    {"name": "Asha", "skills": ["Python"]},
    {"name": "Dev", "skills": ["Java"]}
]

shallow = learners.copy()
shallow[0]["skills"].append("SQL")

print(learners[0]["skills"])  # ['Python', 'SQL']

The new outer list still contains references to the same dictionaries, and the dictionaries still contain references to the same skills lists. The append therefore changes data visible through the original structure too.

Shallow copy is not broken; it follows a specific rule. It creates a new outer compound object and inserts references to the original object's contents. That is often efficient and exactly what you want when nested objects are intentionally shared.

Use deepcopy when nested mutable objects must be independent

copy.deepcopy() recursively copies compound objects it encounters. For the nested learner example:

independent = copy.deepcopy(learners)
independent[0]["skills"].append("SQL")

print(learners[0]["skills"])      # ['Python']
print(independent[0]["skills"])   # ['Python', 'SQL']

Now the nested dictionaries and lists are copied too, so the edit is isolated. Deep copying can take more time and memory than a shallow copy, and it may duplicate objects you intended to share. Use it when that independence is required, not as a default replacement for assignment.

Choose the operation by the data you plan to change

SituationUseful approachWhat stays shared?
Both names should refer to the same listAssignment, such as alias = itemsThe entire object
Copy a flat list of numbers or stringsitems.copy() or items[:]References to any contained objects
Copy the outer collection but intentionally share nested valuesShallow copyNested objects
Independently edit nested mutable valuescopy.deepcopy(items)Usually no nested mutable objects, subject to custom copy behavior

Before copying, ask: which part will I mutate? If only the outer list changes, a shallow copy may be enough. If you will mutate dictionaries or inner lists too, a shallow copy leaves those nested objects shared.

Common copying mistakes

  • Using assignment as a copy. new_list = old_list creates another reference to the same list.
  • Assuming a shallow copy duplicates everything. It duplicates the outer collection, not every object inside it.
  • Deep-copying every value automatically. This may copy more than needed and can be expensive for large structures.
  • Confusing equality with identity. Two lists can contain equal values while still being separate objects. Use is to check identity and == to compare values.
  • Copying resources that are meant to be shared. Files, sockets, modules and similar runtime objects do not behave like ordinary lists; the copy module treats many of them specially or leaves them unchanged.

Small experiment: inspect identity at each level

original = [["Java"], ["Python"]]
outer_copy = original.copy()
deep_copy = copy.deepcopy(original)

print(original is outer_copy)          # False
print(original[0] is outer_copy[0])     # True
print(original[0] is deep_copy[0])      # False

These checks show the difference directly: the shallow copy has a different outer list but shares its first inner list; the deep copy has a separate nested list.

Practical checklist

  1. Is the object mutable? Immutable values such as integers and strings do not need independent mutation.
  2. Does assignment intentionally share the object? If yes, keep the reference.
  3. Will you mutate only the outer collection? A shallow copy may be sufficient.
  4. Will you mutate nested lists, dictionaries or custom objects? Consider a deep copy or explicitly copy just the fields you need.
  5. Could copying too much break shared references or increase cost? Prefer a targeted copy when the data model is known.

Continue learning Python

Understanding references and mutability makes Python collections easier to reason about. Try the examples with your own nested data and observe which changes reach the original. For structured study, explore the Python Programming course details and the Jaipur Engineers course catalog.

Official Python reference

The Python documentation explains assignment, shallow copies, deep copies, recursive objects, the memo dictionary and custom __copy__() or __deepcopy__() methods in the copy module reference.

Want a structured Java learning path?

Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.

Explore the Java program   Ask for course guidance