Skip to content

5. Lists and Dictionaries

A list holds an ordered collection of values — perfect for an inventory:

inventory = ["torch", "rope"]
inventory.append("sword")
print(inventory)
['torch', 'rope', 'sword']

Loop over a list with for:

for item in inventory:
    print(f"You are carrying a {item}.")

A dictionary holds values looked up by name (a key) instead of by position — perfect for a room's exits:

exits = {"north": "hallway", "south": "cave"}
print(exits["north"])
hallway

Try it

Build an inventory list with three items, and an exits dictionary with two directions. Print the inventory with a loop, then print one exit by name.

Tip

A list keeps order and allows duplicate values. A dictionary's keys must be unique — assigning to an existing key replaces its value instead of adding a new one.

Next: Functions →