Learning LibraryCore Coding LibraryKids

List methods: add and remove things

A list can grow and shrink while your program runs. .append(...) adds a new item to the end, and .remove(...) takes one out.

The big idea

A list method like .append() or .remove() changes what is inside a list.

See it in code

1Start simple

.append(...) adds a new item to the end of a list. Here we start with three star spots and add a fourth on the right:

python
spots = [120, 250, 380]
spots.append(440)
print("Spots:", spots)
Run it — the list grew by one:
Spots: [120, 250, 380, 440]

440 landed at the end. The list is longer now — four spots instead of three.

2A step further

.remove(...) takes an item out by its value. Here we pull the middle spot, 250, from that four-spot list:

python
spots = [120, 250, 380, 440]
spots.remove(250)
print("Spots:", spots)
Run it — the `250` is gone:
Spots: [120, 380, 440]

The list closed up around the gap. Now let's do both — add and remove — then draw a star at every spot left.

3In our world

We keep our star spots in a list. .append(440) adds a spot on the right, .remove(250) takes the middle one away, and then we loop over whatever is left and draw a star at each spot:

python
from art import Canvas, star

screen = Canvas.create()
Canvas.fill(screen)

spots = [120, 250, 380]
spots.append(440)
spots.remove(250)
print("Star spots:", spots)

for x in spots:
    star(screen, x, 250, size=60)
Run it — one spot added, one taken away:
Star spots: [120, 380, 440]
Three yellow stars on a dark canvas, grouped toward the right side.

We started with three spots. .append made four, .remove took it back to three — but a different three, just like the warm-ups. The list changed, so the picture changed.

The same idea, everywhere

Adding and removing is how lists stay up to date. A game spawns an enemy with .append and removes it when it's hit. A to-do app adds and crosses off tasks. The list grows and shrinks to match what's really there.

Try it yourself

Add one more spot with .append(60), then run again. Next, try .pop() with no number inside — it removes the last item and hands it back to you.

The common mistake

Trying to .remove something that isn't there. spots.remove(999) crashes, because there is no 999 in the list to take out. Remove only values you know are inside.

What it unlocks

Changing lists builds on lists themselves, and pairs with list indexing to reach an exact spot.

Older, or want more depth? Read the Teens version →