Why Copying a Python List with = Doesn't Actually Copy It

.copy() and [:] make a genuine second list — and why hoard = hoard.sort() empties it.

  • Lesson 5 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Real copies, and the None trap

  1. The dragon: You know treasury = hoard shares ONE list. Here's how to actually get a second, independent one.

  2. The dragon: hoard.copy() builds a brand new list with the same items — mutating the copy never touches the original.

    example.pyPython 3
    hoard = ["gold", "gems"]treasury = hoard.copy()treasury.append("sword")print(hoard)
    Output['gold', 'gems']
  3. The dragon: hoard[:] — a slice with nothing on either side of the colon — does the exact same thing. Both are real copies.

    example.pyPython 3
    hoard = ["gold", "gems"]treasury = hoard[:]print(treasury == hoard, treasury is hoard)
    OutputTrue False
  4. The dragon: Now a completely different trap. Some list methods change the list AND hand you back nothing.

  5. The dragon: hoard.sort() sorts hoard in place — and returns None. Assigning that None back to hoard destroys it.

    example.pyPython 3
    hoard = ["gems", "gold"]hoard = hoard.sort()print(hoard)
    OutputNone
  6. The dragon: The fix: call .sort() on its own line, with nothing capturing its result. hoard is already sorted — no reassignment needed.

    example.pyPython 3
    hoard = ["gems", "gold"]hoard.sort()print(hoard)
    Output['gems', 'gold']
  7. The dragon: .append(), .reverse(), and .sort() all work this way: they mutate and return None. Never write x = x.append(...) either.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    hoard = ["b", "a"]. hoard = hoard.sort(). What does print(hoard) show?

    question.pyPython 3
    hoard = ["b", "a"]hoard = hoard.sort()print(hoard)
    • NoneCorrect
    • ['a', 'b'] — sorted correctly
    • ['b', 'a'] — unsorted, because sort failed

    Why: .sort() genuinely sorts the list in place, but its RETURN VALUE is None — that's true of append() and reverse() too. hoard = hoard.sort() takes the correctly-sorted list and immediately overwrites it with None. The fix is just hoard.sort() on its own, no assignment.

  2. Question 2 of 2

    hoard = ["gold"]. treasury = hoard.copy(). treasury.append("gems"). Does hoard change?

    question.pyPython 3
    hoard = ["gold"]treasury = hoard.copy()treasury.append("gems")print(hoard)
    • No — hoard still just says ['gold']Correct
    • Yes — hoard becomes ['gold', 'gems'] too, same as with plain =

    Why: .copy() (and hoard[:]) are the actual fix for s3's aliasing surprise: they build a second, independent list, so treasury and hoard no longer point at the same thing.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    hoard = ["gold", "gems"]. This time, make treasury an ACTUAL COPY with hoard.copy() — not treasury = hoard. Then treasury.append("sword") and print(hoard). It should NOT have the sword.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.