3. Lists

A list holds lots of values in one box, in order. In your evolution sim you’ll keep all your creatures in a list, and all your food in another list.


Making a list

Use square brackets [ ], with commas between the items.

creature_names = ["Spike", "Bramble", "Pip"]
print(creature_names)

Output:

['Spike', 'Bramble', 'Pip']

A list can hold numbers too:

energy_levels = [100, 75, 40, 90]

An empty list

Often you start with an empty list and fill it up later:

creatures = []       # no creatures yet

Adding to a list

Use .append() to add one item onto the end.

creatures = []
creatures.append("Spike")
creatures.append("Bramble")
print(creatures)

Output:

['Spike', 'Bramble']

How long is the list?

len() tells you how many items are in a list.

creatures = ["Spike", "Bramble", "Pip"]
print(len(creatures))

Output:

3

This is handy: len(creatures) is how many creatures are alive right now.


Getting one item — indexes

Each item has a position number called its index. The tricky part: indexes start at 0, not 1.

List:    ["Spike", "Bramble", "Pip"]
Index:      0          1        2

Get an item with square brackets and its index:

creature_names = ["Spike", "Bramble", "Pip"]

print(creature_names[0])   # the FIRST one
print(creature_names[1])   # the SECOND one
print(creature_names[2])   # the THIRD one

Output:

Spike
Bramble
Pip

You can also count from the end using negative numbers. -1 is the last item:

creature_names = ["Spike", "Bramble", "Pip"]
print(creature_names[-1])   # the LAST one

Output:

Pip

Changing an item

Put a new value into a position:

energy_levels = [100, 75, 40]
energy_levels[2] = 90        # change the third item
print(energy_levels)

Output:

[100, 75, 90]

Removing from a list

.remove() takes out the first item that matches the value you give it:

creatures = ["Spike", "Bramble", "Pip"]
creatures.remove("Bramble")
print(creatures)

Output:

['Spike', 'Pip']

.pop() removes an item by its index and hands it back to you:

creatures = ["Spike", "Bramble", "Pip"]
first_creature = creatures.pop(0)   # remove item at index 0
print(first_creature)               # Spike
print(creatures)                    # what's left

Output:

Spike
['Bramble', 'Pip']

Is something in the list?

The word in checks whether a value is somewhere in the list. It gives back True or False.

creatures = ["Spike", "Bramble", "Pip"]

if "Spike" in creatures:
    print("Spike is here!")

Output:

Spike is here!

Going through every item (the for loop)

This is one of the most useful things in all of programming. A for loop lets you do something to every item in a list, one at a time.

creature_names = ["Spike", "Bramble", "Pip"]

for name in creature_names:
    print(f"{name} is exploring the world.")

Output:

Spike is exploring the world.
Bramble is exploring the world.
Pip is exploring the world.

Read it out loud: “for each name in the list of creature names, print a message.” The word name is a temporary box that holds one item each time round the loop. You can call it whatever makes sense — for creature in creatures, for food in food_items, and so on.

There’s a whole page on loops next — 4. Loops — this was just a taste.


✅ Quick recap

  • Make a list with [ ]: creatures = ["Spike", "Pip"]
  • Add with .append("Bramble")
  • Count with len(creatures)
  • Get an item with its index (starting at 0): creatures[0]
  • Change an item: creatures[0] = "Spike"
  • Remove with .remove("Spike") or .pop(0)
  • Check with if "Spike" in creatures:
  • Go through every item with for creature in creatures: