Learning LibraryCore Coding LibraryTeens

Lists in Python: the collection behind every wave

A list is an ordered, growable collection. The wave of enemies you update and draw each frame? That is a list of objects — one name for the whole fleet.

The big idea

A list stores an ordered sequence of values under one name, which you can index, grow, and iterate.

See it in code

1The basics

A list is an ordered sequence written between [ ]. Here are three enemy x-positions under one name, wave. len(wave) reports the count:

python
wave = [55, 129, 203]
print("Wave:", wave)
print("Enemies:", len(wave))
Run it — one name holds the whole line-up:
Wave: [55, 129, 203]
Enemies: 3

wave is one handle on all three values, in order. len gives the size — here, 3.

2A step further

A list can grow, and you can iterate it. append adds one more to the end; a single for loop then walks every value in the collection:

python
wave = [55, 129, 203]
wave.append(277)
for x in wave:
    print("enemy at x =", x)
Run it — four values now, and the loop visits each:
enemy at x = 55
enemy at x = 129
enemy at x = 203
enemy at x = 277

append grew the list to four; the loop's body ran once per value. Swap those numbers for real objects and nothing else changes.

3In our world

Now the wave is a literal list of Enemy objects instead of bare numbers. wave.append(...) adds one more, len(wave) reports the count, and the same for loop walks the whole collection to draw it:

python
from game import Stage, Enemy

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

wave = [Enemy(55, 90), Enemy(129, 90), Enemy(203, 90)]
wave.append(Enemy(277, 90))

print("Enemies in the wave:", len(wave))
for enemy in wave:
    enemy.draw(screen)
Run it — the list holds four enemies, and the loop draws them all:
Enemies in the wave: 4
A row of four purple enemy sprites evenly spaced near the top of a dark play area.

The list started with three enemies; append made it four — exactly like the warm-up, just with objects. One collection, one loop: whether it holds four enemies or forty, the drawing code never changes.

The same idea, everywhere

Lists are the default container in Python: a series of prices in a backtest, the codons of a gene, the pixels of a row, the moves in a game's history. Any time you have many of the same kind of thing and care about their order, reach for a list.

Try it yourself

Add a fifth enemy with another append, then print wave[0] and wave[-1] to peek at the front and back. Next, build the same wave with a list comprehension: [Enemy(55 + i * 74, 90) for i in range(4)].

The common mistake

Confusing a list with its contents. wave is the box; wave[0] is the first enemy inside it. Calling wave.draw(screen) fails — the list has no draw; each enemy does, which is why you loop.

What it unlocks

Lists lead straight into list indexing, list methods like append and pop, and list comprehensions that build a list in one line.

Want the simpler version? Read the Kids version →