CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 08 / BEGINNER PYTHON

Python lists and dictionaries: organise a case file

A list keeps items in order. A dictionary connects keys to values. Choose the structure that matches the question you want to ask.

The idea

A list is useful for the sequence of places you visited. A dictionary is useful when you want to look up a fact by a name, such as the next scene after a choice. Lists use numbered positions starting at zero; dictionaries use keys you choose. This is how a few ordinary data structures can describe an adventure without hiding the story inside a tangle of decisions.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
places = ["shop", "station", "annex"]
clue = {"owner": "Tony", "place": places[0]}
print(places[1])
print(clue["owner"])
print(clue["place"])
Run and change this example ↗
Reveal the expected output
station
Tony
shop

Read it one step at a time

  1. places[1] selects the second list item because the first position is 0.
  2. The dictionary uses braces. Each colon connects a key to its value; commas separate entries.
  3. clue["owner"] asks for the value under owner. The string key is different from a list position.

Make one change

Change the stored place from places[0] to places[2]. Which line changes?

Check your thinking

Only the final line changes, from shop to annex.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.