Tutorpip← About this lessonOpen the sheet →Download PDF
Ages 10+ · Coding · Python
Lists in Python
The list• snacks = ["mango", "pear",•   "plum", "fig"]The boxes• box 0: mango• box 1: pear• box 2: plum• box 3: fig
Four snacks in four numbered boxes.
The big ideaA list keeps many things in numbered boxes, and the first box is zero.

How it works

A list keeps many things under one name, in a row of numbered boxes, inside square brackets.

Python starts counting its boxes at zero, not one. The first box is box zero, no steps from the start.

Len counts the boxes. The count is one more than the last box number.

Append adds one thing at the end, and a for loop can visit every box in turn.

Make the listAdd the kiwiCount the boxesFetch the lastbox
Mina's snack list grows.

Example 1: Fetching a box

  1. The list is snacks = ["mango", "pear", "plum", "fig"].
  2. Box zero holds the mango, box one the pear, box two the plum and box three the fig.
  3. Asking for snacks[1] means box one, so out pops the pear.

Example 2: Counting the boxes

  1. The list holds four snacks.
  2. Len of snacks says four.
  3. The last box is number three, one less than the count.

Example 3: Adding at the end

  1. Teo brings a kiwi, and Mina uses append.
  2. A fifth box appears at the end, and the kiwi is in box four.
  3. Len now says five, and the last box is number four.

Example 4: Visiting every box

  1. A for loop visits each box in turn, from box zero.
  2. Mango, pear, plum, fig, kiwi: nobody is missed, and it takes only two lines.
  3. Even a hundred snacks would need only two lines.

Words to know

list
Many things kept under one name, in a row of numbered boxes.
box number
A number that says which box to open. It counts from zero.
len
Counts the boxes in a list.
append
Adds one new box at the end of a list.
for loop
Repeats lines, once for every box in a list.

Watch out

Try it

  1. A list holds mango, pear, plum and fig. Which snack is in box two?
  2. A list has five things. What is the number of the last box?
  3. A list has seven things. What does len say?
  4. Teo uses append to add a kiwi to a list of four. Which box holds the kiwi?
  5. Which line asks for the pear in snacks: snacks[0] or snacks[1]?
Answers: 1. plum · 2. 4 · 3. 7 · 4. Box four · 5. snacks[1]
Try at homeWith a grown-up, write four things, such as snacks, on four paper boxes, and number the boxes from zero. Ask them to say a number, and you hold up that box. Then add a fifth box at the end, just like append.
For grown-upsDo the Mission: write four things on four paper boxes, number them from zero, and ask your child to hold up the box you name. Then add a fifth box at the end, like append.
Open these notes on screenScan the code, or go to tutorpip.com.