# Lists

10.07.2017

Episode #8 of the course Coding сoncepts with Scratch by Alan Cohen

This lesson introduces you to lists. In Lesson 5, you learned about variables. They are useful but can only hold one value at a time. Your program may need to hold many related values, and it can get confusing if you have many variables. If you have 20 tasks to complete, you don’t want 20 variables. You want to use a list that can hold all 20 tasks.

Creating a List

Creating a list is like creating a variable:

1. Click Data on the Blocks palette.

2. Click the Make a List button.

3. Enter a name for the list and click the OK button.

Each task in a list is an item. The following example illustrates how to populate a list. The program begins with some explanatory text and asks the user how many tasks they must enter. The program uses a Repeat block to ask the user to enter the specified number of tasks.

Here is the complete program.

Items in a list are entered sequentially. The first item entered is at the top of the list and the last item entered is at the bottom of the list.

Inserting an Item

Lists change. When you add an item to a list, it becomes the last item in the list. However, you may need to insert an item in the middle of the list or at the top of the list. The current list has five items. The new item, “Meet with researcher for updated info for science project,” is a top priority. It needs to be the first item in the list.

The Insert At block lets you insert an item at the location you specify. Select the position from the drop-down list or enter the list position.

Here is the complete program.

The new item is at the top of the list.

Replacing an Item

You may need to replace an item on a list. This allows you to edit an item or change it completely. Item #3, “Buy books to read,” needs to change to, “Buy mystery books to read and magazines.” Use the Replace Item block to do this. Enter the item number and then the replacement text.

Here is the complete program.

Here’s the revised list.

Deleting an Item

The best thing about a to-do list is deleting items after you complete them. Since “Meet with researcher for updated info for science project” is the top priority, once that task is completed, it can be deleted. Use the Delete Of block to do this. Enter the item number or select the number from the drop-down list.

Here is the complete program.

Here is the revised list.

Congratulations! You learned how to create a list, add an item to it, replace an item, and delete an item. The next lesson shares some thoughts and tips about programming.

Recommended book

Scratch 2.0 Game Development HOTSHOT by Sergio van Pul and Jessica Chiang

Share with friends