LightMySky

Lists: Many Values Under One Name

Holding a whole collection of values in a single variable, then reading and changing items by their position.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Create a list and read an item by its index
  • Add and remove items, and say how the positions shift
  • Explain why one list beats twenty separate variables for the same data

1 · Read

One variable can hold a whole row of values called a list. A list of rainbow colours holds red, orange, yellow, green, blue, indigo and violet under a single name. That beats juggling seven separate variables.

Try it together

Each slot has a seat number starting at 0. rainbowColours[0] gives red and rainbowColours[6] gives violet. Watch your seats, since off-by-one slips love list code.

You can rewrite any slot by its seat, so rainbowColours[4] = cyan swaps blue for cyan. To grow the end you append, like pushing Wash the dishes, Do laundry and Mow the lawn onto an empty chore list. New slots take the next free seats: 0, 1 and 2. Slipping one in earlier pushes every later slot one seat up.

Good to know

Pick one list over twenty loose variables whenever the values belong together. One name, many values, zero clutter.

A list stores many values under one name, and you read, rewrite and grow slots by their seat numbers.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Lists: Many Values Under One Name · Computing, ages 15 to 16 · LightMySky