Lists & indexing · About 9 minutes

The creature collection

The big little idea

A list stores several values in order. Use square brackets and separate the items with commas. A list can grow as your adventure grows.

pets = ["dragon", "otter", "robot"]
print(pets[0])
pets.append("unicorn")
print(len(pets))

Python starts counting positions at zero. pets[0] is the first item, pets[1] is the second. append() adds an item to the end. len() tells you how many items there are.

Predict, then try: read the example from top to bottom. What do you think it will print? Copy it into the editor below to test your prediction.

Your turn, code explorer.

Add phoenix to the end of the creatures list. Print the second creature, then the number of creatures. Your output should be otter and 4 on separate lines.

otter
4
my_adventure.pyPython · in your browser

Edit the code. Then run it. Ctrl / ⌘ + Enter also works.

OutputYOUR IDEAS, ALIVE
Your program’s output will appear here.

Ready when you are. ✦

The first run loads Python. This can take a moment.

Program input For programs that use input()

Add answers before you run. Each input() reads the next line. Use made-up names and details.

Real Python, powered by Pyodide. Standard-library programs work here; desktop windows, turtle graphics, and package installation are not supported by this editor. Programs stop after 10 seconds of execution.

Back to the starting code?

This replaces your edits for this activity. Download your code first if you want to keep it.

A little hint, please

Use creatures.append("phoenix"), then change the index to 1.

Show one possible solution

There can be more than one way. Read this version, then try writing it yourself.

creatures = ["dragon", "otter", "robot"]
creatures.append("phoenix")
print(creatures[1])
print(len(creatures))
Pack this idea for later: Lists keep a collection together. The first item lives at index 0.