Python append() and remove(): Changing a List

append, remove — and the surprising truth about copying a list.

  • Lesson 3 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

Changing a list

  1. The dragon: Lists can change AFTER you make them. append adds; remove takes away. Both change the list IN PLACE.

  2. The dragon: list.append(item) adds one item to the end.

    example.pyPython 3
    hoard = ["gold", "gems"]hoard.append("sword")print(hoard)
    Output['gold', 'gems', 'sword']
  3. The dragon: list.remove(item) takes ONE matching item out, by its VALUE — not by counting a position.

    example.pyPython 3
    hoard = ["gold", "gems", "sword"]hoard.remove("gems")print(hoard)
    Output['gold', 'sword']
  4. The dragon: Now the surprising part. treasury = hoard does NOT make a copy — it makes treasury just another name for the SAME list.

    example.pyPython 3
    hoard = ["gold", "gems"]treasury = hoardtreasury.append("sword")print(hoard)
    Output['gold', 'gems', 'sword']
  5. The dragon: hoard shows the sword too, even though you only touched treasury — because hoard and treasury were never two separate lists. They're one list with two names.

    example.pyPython 3
    hoard = ["gold", "gems"]treasury = hoardtreasury.append("sword")print(hoard)
  6. The dragon: This is only true for LISTS, because they're mutable. Numbers and text don't do this — but this is a genuinely important thing to know about lists specifically.

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 1

    hoard = ["gold"]. treasury = hoard. treasury.append("gems"). What does print(hoard) show?

    question.pyPython 3
    hoard = ["gold"]treasury = hoardtreasury.append("gems")print(hoard)
    • ['gold', 'gems'] — hoard shows the change tooCorrect
    • ['gold'] — hoard is unaffected, only treasury changed
    • This causes an error, because treasury was never really defined

    Why: hoard shows 'gems' too — hoard and treasury were always the same list underneath, just with two different names pointing at it. Changing the list through EITHER name changes the one thing both names refer to.

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

    A row of treasures. look() tells you the name of what's on the dragon's tile. The first step is done: walk, then bag.append(look()). Do the same for the other two tiles, then print(bag) — and watch the bag fill up in the dragon's memory as it walks.

  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.