Skip to main content

List operations in Scratch


Hey Scratchers. During our adventure with Scratch, we have already learned that variables can be used to store data in applications. Today we would like to show you how to work with lists. In short, a list is a data structure that contains a collection of elements. We will learn basic list operations and after these exercises you should be ready to use it in your applications.






To create a list, click the Variables button and Make a List button. We add a name for our list as names. Then we observe an interesting thing. A list with a set of possible operations has been added to Scratch. In the preview section we can observe that Scratch added graphic component and thanks to it, we will be able to observe all the elements in the list.



Now, if we want to add some elements to list, we need to drag and drop add… to... component. When our program runs, we want to clear everything, so every time we click the green flag we will get the same result. You can see that the code adds 3 names to the list.


If we think that we forgot to add something then... we are not lost. Thanks to the insert...at... component, we can smoothly insert a new name in the desired place. In our example, we want to insert the new name Jessica in the second position between John and Nicole.


Our final goal is to remove names. Let's assume that Dean was added by accident. We can use the remove…from… component to solve the problem. When we click the green flag, we will see that the names again contain 3 elements.


Today we learned how to create a list and add new items. We also went through operations to modify it, such as insertion and deletion. I hope that you liked it!. 

See you next time!

Comments