Lists in Scratch
A variable can hold one thing at a time, like a single score. But what if you want to keep a whole shopping list, or the names of every player, or all the words for a quiz? For that you use a list. A list is like a numbered notebook: line 1, line 2, line 3, and so on. Lists live in the orange Variables category, right below the variable blocks.
Making a list
Click Make a List, give it a name such as scores or fruits, and choose whether it is for all sprites or just one sprite. A little list box appears on the stage so you can watch it fill up.
Useful list blocks
- add to list — puts a new item at the end of the list.
- delete of list — removes the item at a chosen position.
- delete all of list — empties the whole list.
- insert at — puts an item at a chosen position, pushing the others down.
- replace item of list — changes the item already at a position.
- item of list — reports what is stored at a position.
- length of list — tells you how many items there are.
- list contains — reports true or false if an item is in the list.
Counting from one
Scratch counts list positions starting at 1, not 0. So the first item is item 1, and the last item is at the position given by length of list. This is friendlier for beginners than many other languages.
Going through a list
To look at every item, pair a repeat loop from the orange Control category with a counter variable. Set a variable named i to 1, then repeat: say the item (i) of list, then change i by 1. This walks through the list one item at a time.
Lists are perfect for high-score tables, spelling games, to-do apps, and any project where you need to remember many things instead of just one.