Lists in Scratch 2

Scratch ListsVariables in Scratch allow us to store a single piece of data that we can use in our scripts. This blog post will introduce Lists (also known as arrays in other programming languages). Where variables allow us to store a single piece of information, lists allow us to store multiple pieces of information in the same way we would make a list of items on a sheet of paper. One of the most common lists we make in our lives is a shopping list, and I will use the idea of a shopping list to explain the concept of Scratch lists.

When we write a list on paper, we very often add items to the list from top to bottom, i.e. we add item 1, then item 2, etc. We can do this with lists in Scratch, and we can also insert items at a specific location in the list, remove items from a list, etc. In this post we will cover all the actions you can perform on a list. Code blocks will also be given to show exactly how you express this in the Scratch environment.

Creating a List

To create a list in Scratch, from the Block palette select Data and then click the Make a List button, give your list a name and then click OK, an empty list is then displayed on the Stage. At this point you are then ready to start adding items to your list. In all the example code blocks in this post, the list will be called ShoppingList.

Adding Items

To add an item to our list, we use the add block, which looks like:

Add Block
Where we see the word thing, we replace this with the word, number or sentence we want to add to the list. Looking at the picture of the list at the beginning of this post, we can see the first item in the list is Apples, so we replace thing with Apples, and we then have our first item in the list. If we then add another item, say Oranges, you will see that this is added to the bottom of the list. As we keep adding, the list grows. In programming terms, a list is dynamic in size, meaning it is not a fixed length.

As we are building a shopping list, we want to be able to add many items to it. The code block below allows this to happen. All the code is doing is repeating a block of code asking the user what they want to add to the list. If the user enters the word finish the code block stops.

Adding to a ListInserting Items

So, as we can see its very easy to add items to the end of a list. What about if we want to insert an item within our list? Well this is very easy to do by using the insert block. The insert block allows us to specify what we want to insert and the position in the list where we would like to insert it. If you click on the drop down on the insert block, you will see by default the options 1, last and random. Selecting 1 will insert the item at position 1 in the list, moving the current items in the list down by 1 position (so the current item 1 becomes item 2, etc), last will insert at the end of the list (the same as adding) and random will insert into a random position in the list. We are able to specify the exact position in the list we would like to insert our item. The code block below shows an example of inserting into a list.

List Insert
We simply specify what we would like to insert into the list and the position we would like to insert to. So, using the example of our list at the beginning of this post, if we inserted Milk at position 3, our list would then look like:

1. Apples
2. Oranges
3. Milk
4. Grapes
5. Bread
6. Cheese

As you can see, everything that was in position 3 onwards in the list has now moved down one position.

Deleting from a List

Deleting, or removing items from a list is achieved by using the delete block. You specify the item number you want to delete from the list. By default, the drop down in the block contains 1, last and all, but you can replace this with your required item number. the option all will delete every item from your list (essentially emptying the list), and this is something I would recommend that you do before at the beginning of your scripts. Generally you will always want your scripts to work with an empty list. The code block below shows how to delete an item from a list.

List DeleteReplacing items in a List

A nice feature provided in Scratch is the ability to replace an item in a list with something else. This is basically the same as performing the following two things:

1. Remove an item at a specific position
2. Insert an item at a specific position

The replace does this for us using the replace block. We simply provide the replace block with the location of the item in the list we wish to replace, and the item to replace it with. For example, again if we use our list given at the beginning of this post and we could replace Oranges at position 2 with Pears by simply using the block:

Replace Block
A slightly more complete script block could be as follows:

List ReplaceWhat else can we do with a List?

So, we have covered Inserting, Adding, deleting and Replacing items in a list, what else can we do? There are three more important things we can do with a list, these are:

1. Get an item at a specific position
2. Get the length of a list
3. Search a list for an item

Again, as with all the other list functions, there are blocks that allow us to do the three things. Lets have a look at these in order.

Getting an item at a specific position

Say we want to display an item at a specific position in the list, we use the item block. With this block, we specify the position of the item we want to return and this can then be stored in a variable or you can display it, etc. For our example we will display item number 3 (Grapes in our original list). To do this, we can use the following code block:

List ItemGetting the length of a list

As I’ve already mentioned, the size of a list is dynamic, meaning that the more you add, the bigger it becomes and there will come a time when you want to find out just how long your list is. You can find out the length of your list by using the length of block. In the same way you can return an item in a list to a variable, you can return the length of your list to a variable or again, display it. So, to display the length of our shopping list, we could use the following code block:

Length OfSearch a list for an item

Finally, we can search a list for an item. As you can imagine this is a very useful thing to be able to do. You can do this by using the contains block. The code block below, shows an very simple example of searching for an item in your list.

List ContainsConclusion

This has been a brief introduction to using lists in Scratch. I do hope you find it useful. Feel free to comment on this, use it in your teaching or any way you see fit.

I have produced a very simple Scratch project that was built to help write this post. The link for this is http://scratch.mit.edu/projects/11152013/

Advertisements

2 thoughts on “Lists in Scratch 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s