Back to Python & Coding

PYTHON & CODING · QUESTION 83

What is the difference between a shallow copy and a deep copy?

On this page
Intermediate 4 min read
Interview-ready answerThe concise version to remember

A shallow copy creates a new outer container but keeps references to the same nested objects. A deep copy recursively copies nested objects where possible. Mutating a nested value can therefore affect both shallow-copied structures, while a deep copy usually isolates them. Deep copying can be expensive or inappropriate for resources and custom objects, so choose based on the ownership contract.

LET'S MAKE IT SIMPLE

First, the idea in plain English

Copying a list of simple numbers makes a separate list of values. But if a list contains another list, a shallow copy duplicates only the outer list; both containers still point to the same inner list. A deep copy tries to duplicate nested contents as well.

A USEFUL COMPARISON

Picture it this way

A shallow copy is like making a new folder that contains shortcuts to the same documents. A deep copy duplicates the documents too. If the documents are shared intentionally, duplication may waste space; if independent edits are expected, sharing can cause surprises.

FOLLOW THE JOURNEY

Outer containers can be copied while nested references remain shared

Compare object references created by shallow and deep copying.

Outer containers can be copied while nested references remain sharedCompare object references created by shallow and deep copying.Object-reference diagram
01Inspect nestingIdentify which values are mutable objects referenced by the structure.
02Copy outer containerA shallow copy duplicates the immediate container.
03Copy nested structure if neededA deep copy recursively duplicates reachable objects where supported.
04Test ownership behaviorMutate a nested value and verify the intended sharing contract.

This simplified visual highlights the main relationships. In practice, the exact design depends on the requirements; A copy should match how callers expect to manage state.

A shallow copy creates a new outer list pointing to the same nested list; a deep copy creates a separate outer and nested structure.

How to read this visual
This simplified visual highlights the main relationships. In practice, the exact design depends on the requirements; A copy should match how callers expect to manage state.

UNDER THE HOOD

How it works, step by step

01

Inspect nesting

Identify which values are mutable objects referenced by the structure.

Why it matters: Copy behavior depends on object relationships.

02

Copy outer container

A shallow copy duplicates the immediate container.

Why it matters: Nested references can remain shared.

03

Copy nested structure if needed

A deep copy recursively duplicates reachable objects where supported.

Why it matters: Independent mutation may require separate nested state.

04

Test ownership behavior

Mutate a nested value and verify the intended sharing contract.

Why it matters: A copy should match how callers expect to manage state.

OUTSIDE THE TEXTBOOK

A practical example

A configuration dictionary contains a nested list of allowed regions. A shallow copy used for one request still points to the original region list, so appending a temporary region changes the shared configuration. Deep copy or a targeted copy of that nested list avoids the side effect.

GO A LITTLE DEEPER

Technical details that matter

  • `copy.copy` makes a shallow copy and `copy.deepcopy` uses a memo to handle cycles and preserve shared references where possible.
  • Deep copying may invoke custom `__deepcopy__` methods and does not make external resources safely duplicable.
  • Immutable values can be shared safely, so copying every object is unnecessary.
Python example
import copy

original = {"tags": ["python"]}
shallow = copy.copy(original)
deep = copy.deepcopy(original)
shallow["tags"].append("shared")
print(original["tags"])

The shallow copy shares the nested tags list, so the final output is `["python", "shared"]`. The deep copy has an independent nested list.

MAKE A GOOD CHOICE

When is this useful?

  • Copying nested configurations or request state.
  • Understanding shared references and mutation side effects.
  • Choosing object ownership behavior in APIs.
Consider the alternative

Use immutable data structures or copy only the fields that need independence. A domain-specific clone method may be safer for complex objects.

WATCH OUT FOR

Common misunderstandings

  • Assuming `list.copy()` duplicates nested objects.
  • Deep-copying file handles, locks, or large object graphs blindly.
  • Ignoring shared references and cycles in custom structures.

REAL-WORLD TRADE-OFFS

Limitations and failure cases

  • Deep copying can consume significant time and memory.
  • Some objects cannot or should not be copied.
  • A copy does not isolate external resources or shared state outside the object graph.

WHAT TO REMEMBER

Key takeaways

  1. 1Shallow copy duplicates the outer object; nested references remain shared.
  2. 2Deep copy recursively duplicates nested values where possible.
  3. 3Choose copying based on mutability, cost, and ownership.

PRACTICE THE FOLLOW-UP

What might an interviewer ask next?

Does a shallow copy duplicate immutable values?

It creates a new outer container but can safely reuse immutable values because they cannot be mutated in place.

Why does deepcopy use a memo?

The memo helps handle cycles and preserves relationships among objects during recursive copying.

SAY IT OUT LOUD

How to explain this in an interview

“A shallow copy creates a new outer container but shares nested mutable objects. A deep copy recursively copies nested values, which can isolate mutations but cost more and may not suit resources such as locks or file handles. I would first define the ownership behavior needed, then copy only the structure that must be independent.”

Continue your learning path

Connect this question to the roadmap topic it supports.

Open roadmap topic