Shallow vs Deep Copying

Why a copy of a list of lists still shares its inner lists, and when you need copy.deepcopy.

Overview

Why a copy of a list of lists still shares its inner lists, and when you need copy.deepcopy.

In this guide
  1. What a shallow copy actually does
  2. The three shallow copies
  3. When shallow is enough
  4. When you need deep
  5. The dict version of the trap
  6. The rule
  7. Copying an object, not just a container
  8. Why deepcopy is slower than it looks
  9. Cheaper alternatives
  10. A practical rule for configuration
  11. Watching the difference with is
  12. Where this actually comes up
  13. Equality survives copying; identity does not
  14. Immutability removes the question
  15. Deciding, in four questions
  16. Questions people ask about Shallow vs Deep Copying
  17. Shallow vs Deep Copying: recap in one screen

shallow.py

shallow.py Python 3
Output

                    

copy_when.py

copy_when.py Python 3
Output

                    

Worth knowing

A shallow copy duplicates the outer container and reuses everything inside it.
[:], list(x) and x.copy() are all shallow.
Nested mutable data is when you need copy.deepcopy.
If everything inside is immutable, shallow is enough - there is nothing to share.

Shallow vs Deep Copying: A Practical Guide

Copying a list gives you a new list. It doesn't give you new copies of the things inside it. That gap is where the bugs live.

What a shallow copy actually does

original = [[1, 2], [3, 4]]
shallow = original[:]
shallow[0][0] = 99
print(original)  # [[99, 2], [3, 4]]

The outer list is new. original is shallow is False. Appending to one does not affect the other. But the two inner lists were not copied. Both outer lists point at the same two inner lists. Change something one level down and both see it.

original[0] is shallow[0] is True. That is the whole story in one line.

A shallow copy duplicates the container but not the items inside it. For a flat list of numbers or strings, that is fine. Numbers and strings are immutable, so sharing them causes no problems. You cannot change an integer in place, so it does not matter how many lists point to the same one.

The trouble starts with nested structures. A list of lists, a dictionary whose values are lists, a list of dictionaries: in all of these, a shallow copy gives you a new outer container but the same inner objects. Modifying an inner object through one reference modifies it for all references. That is the same aliasing problem from the previous page, one level deeper.

Python offers three ways to make a shallow copy of a list: a[:], list(a), and a.copy(). All three do exactly the same thing. For dictionaries, d.copy() and dict(d) are the two options. Sets have s.copy(). None of these copy nested objects.

A deep copy duplicates everything, all the way down. The copy module provides copy.deepcopy(obj). It walks the entire structure, copying every container it finds. After a deep copy, no object is shared between the original and the copy. Changing anything in one has zero effect on the other.

Deep copying is slower and uses more memory. For most code, a shallow copy is enough. You need a deep copy only when you have nested mutable containers and need complete independence between the original and the copy. The typical case is when you want to modify a configuration structure without affecting the original, or when you are building a tree of objects that will be modified in place.

This page covers what a shallow copy actually does, why nested structures expose the problem, the three ways to shallow-copy a list, copy.deepcopy for full independence, and when each approach is the right choice.

The three shallow copies

nums[:]    list(nums)    nums.copy()

All equal. dict.copy() and set.copy() behave the same way. dict(d) is the dict equal of list(l).

When shallow is enough

If all inside is immutable. Numbers, strings, tuples of those — a shallow copy is a complete copy in each way that matters. There's nothing shared that can change, so the gap disappears.

That covers most everyday copying. This Is why [:] is so common and why the problem stays hidden until the day your data has a list inside a list.

When you need deep

import copy
deep = copy.deepcopy(original)

deepcopy walks the whole structure and rebuilds each mutable object it finds. Nested config dicts, lists of records, any of it parsed from JSON. These are the cases.

The dict version of the trap

config = {"limits": {"max": 10}}
shallow = config.copy()
shallow["limits"]["max"] = 999

The first now reads 999 too. This is the same rule and it bites harder with configuration. The nesting is the point of the structure.

The rule

Ask what is inside. Flat and immutable: use a slice or .copy(). Nested and mutable: use deepcopy, or restructure so you aren't copying a mutable tree at all.

Copying an object, not just a container

copy.copy works on your own classes too, and it copies the same way: a new object whose fields point at the same values. If an field is a list, both copies share it.

Classes can control this. Defining __copy__ and __deepcopy__ lets an object say how it should be duplicated. This Matters when it holds a thing that shouldn't be copied at all - an open file, a DB link, a lock. In practice you rarely write these. But Knowing they exist shows why copying some library objects behaves in ways a plain field copy wouldn't.

Why deepcopy is slower than it looks

deepcopy walks the entire object graph, duplicates each mutable thing it finds, and keeps a record of what it has already copied so that shared refs stay shared and cycles don't become infinite recursion.

That bookkeeping is why it's slow on large structures, and it's also why it's correct in cases a hand-written recursive copy often gets wrong. If you've ever written a recursive copy helper and hit RecursionError on data that referred back to itself, that is the problem deepcopy already solved.

Cheaper alternatives

Often the goal isn't a copy at all. A modified version of a thing, without disturbing the first. a few types offer that right:

new_config = {**defaults, **overrides}    # merged, both untouched
new_items = [*items, extra]               # extended, original untouched
new_point = point._replace(x=5)           # namedtuple

Each of these makes a new outer object and leaves the inputs alone, at the cost of a shallow copy rather than a deep one - which is just right when the contents are immutable.

If the contents are mutable and you find yourself needing deepcopy often, that is often a signal that the structure would be better as immutable data: tuples, frozensets, or small classes that return new instances rather than mutating themselves.

A practical rule for configuration

Nested dicts loaded from JSON or YAML are the single most common place this bites, because the nesting is the whole point and .copy() looks like it did the job.

If a function takes a config dict and changes any of it inside it, either it should be documented as doing so, or it should deepcopy first. Silently editing a nested value in a structure the caller still holds is the sort of bug that gets diagnosed as "the settings at random change". This Is a long way from where the mutation really happened.

Watching the difference with is

The rule is one line of output away from being obvious:

import copy

original = {"a": [1, 2]}
shallow = original.copy()
deep = copy.deepcopy(original)

print(original["a"] is shallow["a"])
print(original["a"] is deep["a"])

shallow["a"].append(3)
print(original, deep)
True
False
{'a': [1, 2, 3]} {'a': [1, 2]}

The first two lines are the whole gap. After a shallow copy, the inner list is *the same object*. After a deep copy it's a new one. The last line is the consequence: appending through the shallow copy changed the first, and the deep copy was untouched.

Note that the outer dicts are different objects in both cases. A shallow copy is a real copy. Adding a new key to shallow doesn't affect first. It's only one level deep, and each problem on this page comes from the level below.

Where this actually comes up

The trap is rarely met head-on. It arrives through four plain situations.

Configuration loaded from JSON or YAML. These are nested dicts by nature, .copy() looks like it did the job, and a function that adjusts one value has quietly adjusted it for everybody.

A default that is a nested structure. A module-level DEFAULTS dict copied shallowly into each new object means each object shares the same inner dicts. The objects look free and aren't.

A list of records. rows[:] gives a new list of the same dicts, so sorting or filtering the copy is safe and editing a record through it isn't. This is a common one because the copy was made in detail to be safe.

Undo, or "keep the first for check". Snapshotting state before a change is the exact case where a shallow copy fails: the snapshot shares the mutable parts with the thing that is about to change. So By the time you compare, both have moved.

The pattern across all four is that the copy was made for safety, and shallow copying gives that safety only for the top level. If the reason for copying is "so that changes over here don't affect over there", the question to ask is how deep the changes go.

Equality survives copying; identity does not

After any copy, shallow or deep, copy == first is True and copy is first is False. That is the intended action and it's worth being clear about, because it's how you check that a copy did what you meant.

For your own classes it holds only if you defined __eq__. Without one, Python checks by identity, so a copy of your object is *not* equal to the first — which surprises people who have just written a careful __deepcopy__ and find their tests failing on the check rather than the copy. Defining __eq__ on a class whose instances get copied around is in effect needed, and a dataclass writes it for you.

deepcopy also preserves structure that a naive copy would flatten. If two entries in a dict point at the same list, the deep copy has two entries pointing at one *new* list. The sharing is copied rather than duplicated. That is often what you want, and it's one more thing a hand-written recursive copy gets wrong.

Immutability removes the question

The most solid fix for a copying bug is to not need the copy.

If the structure is built from tuples, frozensets, strings and numbers, then nothing inside it can change. So Sharing it's safe and a shallow copy is a complete one. The gap that this whole page is about simply doesn't arise. That is why no one worries about copying a tuple of strings, and why deepcopy on such a structure is wasted work.

The hands-on version is to make the things that travel immutable. A record passed between functions is better as a frozen dataclass or a NamedTuple than as a dict: it can't be edited by a function that received it, so no defensive copy is needed at any boundary. Where a modified version is needed, _replace or dataclasses.replace builds a new one and leaves the first alone.

Keep the mutable structures local. Here Each name pointing at them is visible in the same function. Then copying turns a choice you make at times rather than a defence you've to note all over.

Deciding, in four questions

When you're about to copy a thing and aren't sure which kind you need, the answer follows from four questions asked in order.

Does any of it inside it change? If each item is a number, a string or a tuple of those, any shallow copy is a complete one. Stop here. This covers most copying.

Am I copying so that changes over here don't affect over there? If the answer is no — you're copying to get a list you can sort, or to add one key — then a shallow copy of the outer container is just what you want and nothing more is needed.

How deep do the changes go? If the code that follows only touches the top level, shallow is enough even so of what is nested underneath. If it reaches into nested structures, each level it reaches has to have been copied.

Is the structure large or does it hold a thing uncopyable? deepcopy on a big object graph costs real time, and on an object holding a link, a file or a lock it either fails or duplicates a thing that should be unique. Both are signals to restructure rather than to copy harder — often by making the shared parts immutable, or by rebuilding the small part you need to change rather than duplicating the whole.

Most real answers land on shallow, and the ones that don't are often nested configuration or a snapshot taken for check. Knowing which of those you are in is more handy than a rule about which function to call.

Questions people ask about Shallow vs Deep Copying

Is list(x) a deep copy? No. It's one of the shallow ones, along with x[:] and x.copy().

Does deepcopy handle cycles? Yes. It remembers what it has already copied, so a structure that refers to itself doesn't cause infinite recursion.

Is deepcopy slow? scaled to a shallow copy, a lot. It walks the whole structure and keeps a record as it goes. For small structures it doesn't matter.

How do I copy only two levels? There's no built-in for that. A comprehension that shallow-copies each item — {k: v.copy() for k, v in d.items()}. Is the usual answer.

Can I stop an field being deep-copied? Yes, by defining __deepcopy__ on the class. This Is how objects holding a link or a lock avoid duplicating it.

Does copying a string do any of it? No. Strings are immutable, so copy returns the same object.

What about copy.copy versus .copy()? The same thing for builtins. copy.copy also works on objects that have no .copy() method.

Does deepcopy copy class meanings too? No. Classes, functions and modules are treated as atomic and shared rather than duplicated, which is what you want.

Is pickling and unpickling a valid deep copy? It often makes one, and it's slower and fails on any of it unpicklable. Use deepcopy, which is what it's for.

Why does copying a nested list "at times" work? Because it works when nothing reaches past the top level afterwards. The copy was always shallow. The code simply had not yet touched the shared part.

Should a function copy the args it's given? Only if it stores them or mutates them. A function that reads a list and returns a result has no reason to copy any of it, and copying defensively all over costs more than the bugs it blocks.

What is the fastest way to copy a flat list? x.copy() and x[:] are equal and both are fast. list(x) is the one that also accepts any iterable, which is at times why it's chosen.

Shallow vs Deep Copying: recap in one screen

  • A shallow copy duplicates the outer container and shares all inside it. first[0] is shallow[0] is the test.
  • Shallow is a complete copy when the contents are immutable. This Is why the problem stays hidden for so long.
  • copy.deepcopy rebuilds the whole structure, preserves shared refs and lives cycles — and costs proportionally.
  • The bugs arrive through config dicts, shared defaults, lists of records and snapshots, not through code that obviously copies.
  • Making the data immutable drops the question fully.

Check yourself

0 of 3

Answer without scrolling back up.

  1. After a shallow copy of `[[1, 2]]`, changing `copy[0][0]`:

  2. When is a shallow copy sufficient?

  3. Which of these is NOT a shallow copy of a list?

Cheat sheet

Shallow vs Deep Copying

Copy a list with slicing or copy(), see that the inner objects stay shared, and use deepcopy when you need a fully independent clone.

PYTHON · vizlearn.in/python/shallow_and_deep_copy.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.