Learning Goals 3 min
By the end of this lesson you will be able to:
- Use replace item (1) of [shopping v] with [new] to overwrite the value in one specific slot, without changing the list's length.
- Use insert [thing] at (1) of [shopping v] to squeeze a new item in at any position, pushing everything at or after that slot down by one.
- Use delete (1) of [shopping v] and delete all of [shopping v] to remove either one item or every item, and explain how indexes shift after a single delete.
Warm-Up — Aniq's broken shopping list 7 min
Aniq has a list called shopping with five items his mum dictated this morning. Look at the list:
when flag clicked
delete all of [shopping v]
add [bread] to [shopping v]
add [eggs] to [shopping v]
add [milkk] to [shopping v]
add [rice] to [shopping v]
add [sugar] to [shopping v]
Aniq notices the typo in slot 3 — milkk should be milk. He also remembers his sister asked him to add butter right between bread and eggs — so butter should land in slot 2 and push everything else down. And while he's at it, he no longer needs sugar. Predict — can he do all three of these edits with the add block alone?
Reveal the answer
No. add () to [shopping v] always sticks the new item on the end of the list — it can't replace a slot, can't squeeze into the middle, and can't remove anything. Aniq needs three new blocks: one to fix the typo, one to insert in the middle, and one to delete the unwanted item. Today's lesson is those three blocks.
The add block is the workhorse, but it only knows one trick — push to the end. After today you can edit any list, anywhere, the way a real text editor edits a document.
New Concept — replace, insert, delete 15 min
Open the orange Variables palette and scroll to the list blocks. You've already used add () to [shopping v]. Right below it you'll find three more stack blocks (with notches top and bottom). These are today's stars.
Replace — overwrite without changing length
The first one is replace item (1) of [shopping v] with [new]. It does exactly what it says: take the item at slot 1 and overwrite it with whatever you pass in.
replace item (3) of [shopping v] with [milk]
milkk. After this block, slot 3 says milk. Slots 1, 2, 4, 5 unchanged. List length unchanged.The length of the list does not change after replace. You had 5 items before, you have 5 items after. Replace is the "fix a typo" or "update a value" block.
Insert — squeeze a new item in at any slot
Second is insert [thing] at (1) of [shopping v]. It puts the new item at that slot, and pushes everything that was there (and everything after it) down by one position.
insert [butter] at (2) of [shopping v]
eggs is now at slot 3, not slot 2.The length of the list goes up by 1 after insert. insert [x] at (1) puts the new item at the very front. insert [x] at ((length of [shopping v]) + (1)) puts it at the very end — but for that last case, just use the regular add block, it's the same thing and shorter.
Delete — remove one item, or all of them
Third is delete (1) of [shopping v]. It removes the item at that slot, and pulls everything after it up by one position.
delete (6) of [shopping v]
sugar is gone; no other item changed slot.The length of the list goes down by 1 after a single delete. Note: in this example we deleted the last item, so nothing shifted upward. If we had deleted slot 1 (bread), then butter would now be at slot 1, eggs at slot 2, and so on — everything would shift up.
Right next to it is delete all of [shopping v]. This one is special and used everywhere — usually as the very first block under when flag clicked to guarantee the list starts empty every run.
when flag clicked
delete all of [shopping v]
Worked Example — fixing Aniq's shopping list 12 min
We're going to clean up Aniq's warm-up list. Three edits: fix the typo, insert butter, delete sugar. Eight steps.
shopping watcher is being replaced (milkk → milk) while length stays at 5. The cat (the ✏️ sprite) confirms each edit. This row-level surgery is the move the Sort feature repeats over and over.Step 1 — Start fresh
Open Scratch. Make a list called shopping (Variables → Make a List). Tick its checkbox so you can watch it on the Stage.
Step 2 — Hat + clear
From Events: when ⚑ clicked. From Variables: delete all of [shopping v]. Now the list is empty no matter what.
Step 3 — Add the five original items
Five add blocks in a stack: bread, eggs, milkk, rice, sugar. Yes, type the typo on purpose — we're going to fix it next.
Step 4 — Run once and watch the list grow
Click the flag. The list on the Stage shows all five items in order. Slot 3 says milkk.
Step 5 — Fix the typo with replace
From Variables: replace item (3) of [shopping v] with [milk]. Snap it under the last add. Click the flag again — slot 3 now correctly says milk.
Step 6 — Insert butter at slot 2
From Variables: insert [butter] at (2) of [shopping v]. Snap it under the replace. Click the flag — list is now bread / butter / eggs / milk / rice / sugar. Length 6.
Step 7 — Delete sugar (which is now at slot 6)
From Variables: delete (6) of [shopping v]. Snap it under the insert. Click the flag — sugar is gone.
Step 8 — Sanity check
From Looks: say (length of [shopping v]) for (2) seconds. The cat should say 5. Started with 5, replaced one (still 5), inserted one (6), deleted one (back to 5). The arithmetic of length-changes works out exactly.
The full assembled stack
when flag clicked
delete all of [shopping v]
add [bread] to [shopping v]
add [eggs] to [shopping v]
add [milkk] to [shopping v]
add [rice] to [shopping v]
add [sugar] to [shopping v]
replace item (3) of [shopping v] with [milk]
insert [butter] at (2) of [shopping v]
delete (6) of [shopping v]
say (length of [shopping v]) for (2) seconds
5.What you just built: a tiny list-editor script. You can now edit a list in Scratch the same way you'd edit a document in Word — fix a word, add one in the middle, remove one at the end. That's a real superpower compared to just adding.
Try It Yourself — three list-edit drills 15 min
Goal: Make a list called colours with three colours: red, gren, blue. Then use one replace block to fix the typo — gren should become green.
when flag clicked
delete all of [colours v]
add [red] to [colours v]
add [gren] to [colours v]
add [blue] to [colours v]
replace item (2) of [colours v] with [green]
say (item (2) of [colours v]) for (2) seconds
green. List length unchanged.Think: Why slot 2 and not slot 1 or 3? Look at the order of the adds. The second add was gren, so that's slot 2.
Goal: Build a list called queue for a tuckshop line. Add three students: Aiman, Bavani, Chen. Then a VIP arrives — insert Principal at slot 1 so the head moves to the front of the queue. Show the new list using length reporter.
when flag clicked
delete all of [queue v]
add [Aiman] to [queue v]
add [Bavani] to [queue v]
add [Chen] to [queue v]
insert [Principal] at (1) of [queue v]
say (join [Queue length: ] (length of [queue v])) for (1) seconds
say (item (1) of [queue v]) for (2) seconds
Principal. Aiman pushed down to slot 2.Think: Notice how insert-at-1 is the opposite of add. Add pushes to the end; insert-at-1 pushes to the front. Together they let you build lists from either side.
Goal: Build a "delete by name" mini-tool. The user types a name; if that name is at slot 3 of the guests list, delete it; otherwise say Not found in slot 3. Use () = () to compare the answer with item 3.
when flag clicked
delete all of [guests v]
add [Hafiz] to [guests v]
add [Ila] to [guests v]
add [Jay] to [guests v]
add [Kavi] to [guests v]
ask [Who to remove from slot 3?] and wait
if <(answer) = (item (3) of [guests v])> then
delete (3) of [guests v]
say [Removed!] for (1) seconds
else
say [Not found in slot 3] for (2) seconds
end
say (length of [guests v]) for (2) seconds
Think: If the user types Jay, the delete fires and length drops to 3. If they type Hafiz (who's at slot 1, not 3), the else fires and length stays 4. Real "search and delete" needs L03-12's loop pattern — that's coming.
Mini-Challenge — Lina's double-delete trap 5 min
"Delete slot 2 twice"
Lina wants to remove the two middle items from a five-item fruits list. The list is: apple, banana, cherry, durian, eggplant. She wants to keep apple, cherry, eggplant (slots 1, 3, 5). She writes:
when flag clicked
delete all of [fruits v]
add [apple] to [fruits v]
add [banana] to [fruits v]
add [cherry] to [fruits v]
add [durian] to [fruits v]
add [eggplant] to [fruits v]
delete (2) of [fruits v]
delete (4) of [fruits v]
What does the list actually contain after this runs, and how does Lina fix it?
Reveal one valid solution
After delete (2), banana is gone and the list becomes apple, cherry, durian, eggplant. Cherry is now at slot 2, durian at slot 3, eggplant at slot 4. Lina's next block deletes slot 4 — which is now eggplant, not durian! Final list: apple, cherry, durian. She kept the wrong items.
One fix is to delete from the back first — slot 4 (durian) is deleted before slot 2 (banana), so the earlier indexes don't shift in between:
when flag clicked
delete all of [fruits v]
add [apple] to [fruits v]
add [banana] to [fruits v]
add [cherry] to [fruits v]
add [durian] to [fruits v]
add [eggplant] to [fruits v]
delete (4) of [fruits v]
delete (2) of [fruits v]
Two blocks swapped — that's it. Final list: apple, cherry, eggplant. When you delete multiple slots, always go from highest slot down to lowest, or your indexes will shift under your feet. This is a real gotcha that catches grown-up programmers too.
Recap 3 min
You met the three list-mutation blocks beyond add. replace overwrites one slot without changing length. insert squeezes a new item in and pushes the rest down — length grows by 1. delete removes one item and pulls the rest up — length shrinks by 1. delete all of wipes the whole list and is almost always the first block under your flag-click. Most small projects only need add + delete-all; the other three are for fancier editing where you really do need surgical control of individual slots.
- Replace
- replace item (N) of [list v] with [value] — overwrites slot N. Length unchanged. The "fix a typo" block.
- Insert
- insert [value] at (N) of [list v] — puts new item at slot N, pushes everything from N onward down by one. Length goes up by 1.
- Delete (one)
- delete (N) of [list v] — removes the item at slot N, pulls everything after up by one. Length goes down by 1.
- Delete all of
- delete all of [list v] — empties the list completely. Length becomes 0. Use this at the start of every list-using script.
- Index shift
- What happens after insert or delete: items past the changed slot all move position by one. The reason "delete slot 2 then delete slot 4" almost never deletes what you expect.
- Mutation block
- A stack block that changes the list (add, replace, insert, delete). The opposite of a reporter, which just reads from the list without changing it.
Homework 2 min
The To-Do List Editor. Build a small to-do list that supports four operations: add, edit, remove, and clear.
- At flag click: delete all of [todo v], then add three starter tasks (your choice — e.g.
read book,study,kelas Mandarin). - when [a v] key pressed: ask [New task?] and wait, then add (answer) to [todo v].
- when [e v] key pressed: ask which slot to edit, ask for the new text, then replace item (answer) of [todo v] with (answer). (Yes, two asks — the second one's answer overwrites the first one, so you'll need a temp variable. Try it.)
- when [d v] key pressed: ask which slot to delete, then delete (answer) of [todo v].
- when [c v] key pressed: delete all of [todo v] and say
Cleared!.
Save as HW-L3-11-Todo-Editor.sb3. Hit the flag, then press A / E / D / C in various orders to edit your list live on the Stage.
Bring back next class:
- The
.sb3file. - Your answer to: "Why did the edit (E) handler need a temp variable? What happens if you don't use one?"
Heads up for next class: SCR-L03-12 meets the loop-through-a-list pattern — a counter + repeat + length + item. That one pattern lets you visit every item in a list and do something with each one. Almost every list-using program uses it somewhere.