Introduction to List in SNAP!

We use lists every day. The most common list we use our life is a shopping list. In this blog, we will see how lists can be represented in Computer Science (specifically SNAP!).

Characteristics of Lists

Before we jump into how we represent lists in Computer Science, let’s discuss what are the key characteristics of the lists. Take an example of a regular shopping list, which contains some items like milk, juice, apple and so on. We should be able to perform following activities on this list:

  1. Ability to add items to and remove from this list. This means the length of the list is not fixed. We can add as many items to the list as we want and remove as many items from the list as many we want, and until there are items existing in the list.
  2. Ability to refer to the list with one name. For example, when mom hands over the list to dad, she doesn’t have to repeat individual element in the list and instruct dad to bring milk, juice, eggs and so on. She can tell him to bring everything in the shopping list.
  3. Ability to operate on the individual elements in the list. For example, when dad goes for shopping and purchases eggs, he can strike-off “eggs” from the list, and move onto purchasing the next item. He doesn’t have to strike-off the whole shopping list if he gets only some items from the list and not all.

Now let’s see how we can represent a list with the above-mentioned characteristics in computer science.

Lists in SNAP!

In Computer Science, a list is a means of storing multiple values in a single location. Different programming languages have different ways to represent lists and give them different names. In SNAP!, a list is referred as list and can be created using List block in SNAP! block in Variables pallette.

To create a shopping list in SNAP!, drag and drop the list block on the scripting area and type in one element of the list, for example, milk in the white space. To add elements to the list, click on the right arrow of the list, and it will add another blank element. Type in the next element in the list, for example, juice. Repeat the process for the number of elements you want to add to the list.

Shopping List in SNAP!

Deleting an element from the list is even simpler. Just click on the left arrow in the list and it will delete the last element in the list.

As you can see, using arrows you can add elements only to the end of the list and delete only the last element of the list. You cannot add or delete in the middle of the list. You can achieve this in another way.

Add an Element to the List

There are two ways you can add an element in the list in SNAP!.

  1. Using add block, you can add an element at the end of the list. For example:
    Add Element at the End of the List
  2. Using insert block, you can insert an element at the beginning, end or at random location in the list.
    Insert an Element in the list in SNAP!

Delete an element from the list

To delete an element from the list, you can use delete block. With this block, you can delete the first, last or all elements of the list.

Delete an element from the list in SNAP!

Note: In all the examples above, I created a variable to assign the list and then updated the list with add, delete and insert functions. While running these operations on the list, I was referring to it with a single name. The list block is a single value, although it contains multiple values in itself.

Length of the list

Finding the length of the list is the most common operation in the Computer Science. Irrespective of the language you are writing code in, if you are using lists, the chances are your program is going to need to know the length of the list and perform operations based on that. in SNAP!, you can find the length of the list using length block. For example,

Length of the List in SNAP!

Why not use individual variables instead of list?

Now that we have covered the key concepts of the list, it is easier to answer this question. Why didn’t we end up creating separate variables instead of using the list? We have come full circle here, we came to where we started from. Check characteristics of the list and list block provides us with all those characteristics.

Hope you enjoyed reading this! If you have any questions/feedback, please share it in the comments below.

Share This:

One thought on “Introduction to List in SNAP!

Leave a Reply

Your email address will not be published. Required fields are marked *