Portmint Lighthouse

Lists, Many Things at Once

Here is the plain idea: a list holds many values together, in order, under a single name. Instead of one box for one value, a list is one box that holds a whole row of values, lined up neatly.

Write colors = ["red", "green", "blue"], and the box named colors holds all three, in that order.

The egg-carton analogy

Picture an egg carton. It's one container, but it holds a dozen eggs, each sitting in its own numbered slot. You don't need twelve separate containers — one carton, twelve spots, all in a tidy line.

A list is that carton. One name, many slots. Each slot holds a value, and the slots stay in order, so there's always a first, a second, a third, and so on.

Reaching into a slot

Because the slots are ordered, you can point at any one of them by its position. "Give me the value in the first slot." "Give me the third."

One quirk surprises every beginner: programmers usually start counting slots at zero, not one. The first slot is position 0, the second is position 1, and so on. It feels odd at first, then becomes second nature. Just remember: the opening slot is number zero.

So colors[0] reaches in and hands you "red", the very first value. colors[2] hands you "blue". Reach for a slot that isn't there — colors[9] in a three-value list — and the program stops and tells you there's nothing in that spot.

Lists love loops

Here's where two ideas click together beautifully. Remember loops — repeating steps? A list is the perfect thing to loop over. "For every color in the list, do something with it." The loop walks slot by slot, handing you each value in turn, until the carton is empty.

This pairing is one of the most common patterns in all of programming. Every customer in a list — send each an email. Every number in a list — add it to a running total. Every photo in an album — show it on screen. The list holds the collection; the loop visits each member.

The quiet power is that the same code works no matter how long the list is. Three values or three thousand, you write the step once and the loop covers them all.

Lists can grow and change

An egg carton has a fixed dozen slots, but a programming list is more generous. You can add new values to the end, remove ones you don't want, and change what's in a slot — all while keeping the same name on the front.

A to-do list that grows as you think of tasks, a shopping cart that fills as you browse, a high-score table that updates — all just lists, gaining and losing values over the life of the program. And the values inside don't have to be text: a list can hold numbers, true-or-false values, even other lists.

Your turn

No computer needed — just answer on paper. Imagine a list pets = ["cat", "dog", "fish"].

  1. What does pets[0] hand back? What about pets[2]?
  2. Which value sits at position 1?
  3. Write a plain-English loop sentence that "feeds each pet in the list," one at a time.

(Counting from zero: pets[0] is "cat", pets[2] is "fish", and position 1 holds "dog".) If your answer to the first slot is "cat" — the first value — then counting from zero has clicked.

So: a list is an egg carton, one name holding many ordered values, you reach a slot by its position (counting from zero), and loops are how you visit them all. Next, we'll see how these pieces snap together. 🐙

Stuck or curious?

Ask Pip about this lesson — tap the porthole bottom-right.