Learning LibraryCore Coding LibraryTeens

List Methods in Python: append, pop, and managing a wave

A game's wave is never static — enemies spawn and get destroyed. List methods (append, pop, remove) are how you keep the collection in sync with what's on screen.

The big idea

List methods mutate a list in place: append adds to the end, pop(i) removes and returns the item at index i, remove(x) deletes the first matching value.

See it in code

1The basics

append mutates a list in place, adding to the end. Start with a five-enemy wave (bare x-positions) and spawn a reinforcement on the right:

python
wave = [55, 129, 203, 277, 351]
wave.append(425)
print("Wave size:", len(wave))
print(wave)
Run it — the wave grew from five to six:
Wave size: 6
[55, 129, 203, 277, 351, 425]

No reassignment — append changed wave itself. len confirms six now.

2A step further

pop(i) removes the item at index i and returns it — that return value is the difference from remove. Here a laser takes out the enemy at index 2:

python
wave = [55, 129, 203, 277, 351, 425]
destroyed = wave.pop(2)
print("Removed:", destroyed)
print("Wave size now:", len(wave))
Run it — one gone, and we still hold what it was:
Removed: 203
Wave size now: 5

pop(2) shrank the list to five and handed back 203. Now the same two moves, on real enemies you can animate.

3In our world

Same moves, now on Enemy objects. append spawns a reinforcement; pop(2) removes the enemy at index 2 and hands that object back, so you could animate its explosion. Then we draw whatever remains:

python
from game import Stage, Enemy

screen = Stage.create()
Stage.clear(screen)

wave = [Enemy(55 + i * 74, 90) for i in range(5)]
wave.append(Enemy(425, 90))

destroyed = wave.pop(2)
print("Wave size now:", len(wave))

for enemy in wave:
    enemy.draw(screen)
Run it — six spawned, one popped, five drawn with a gap:
Wave size now: 5
Five purple enemy sprites near the top of a dark play area, with a visible gap where one was removed.

append grew the wave to six; pop(2) shrank it to five and returned the removed enemy into destroyed — the same object this time, not just a number. That is the difference from remove: pop gives the item back, so you can keep using it.

The same idea, everywhere

In-place list edits run every game loop and data pipeline: append a new price tick, pop a task off a queue, insert a step into a plan, sort a leaderboard. These methods change the list itself — which is powerful, and the source of one classic bug (below).

Try it yourself

Swap pop(2) for remove(wave[2]) and note it deletes by value, not index. Then try wave.sort(key=lambda e: e.x) to order the survivors left-to-right.

The common mistake

Removing items from a list while looping over it. Deleting elements mid-iteration makes the loop skip neighbors, because the indexes shift under it. Build a new list of survivors, or loop over a copy (for e in wave[:]), instead.

What it unlocks

Mutating methods extend lists and list indexing, and set up the update-each-frame pattern behind enemy waves.

Want the simpler version? Read the Kids version →