Lists: Many Values Under One Name
Holding a whole collection of values in a single variable, then reading and changing items by their position.
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.
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.
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
Where it sits
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.