Working With Lists

Published on February 6, 2022

Lists store a series of data. A string or a number provides a single piece of data, while lists can have dozens of data provided. For example, I might want a list of available types of coffee, or a list of players.

This guide does not cover all list blocks, but introduces some of the basics. I selected the easiest and the moderates to introduce.

For more information, read this MIT App Inventor community thread.

🏷️ Tags: #tutorials-and-guides

The create empty list block creates an empty list. It has 0 elements, therefore has 0 indexes.

To understand an index, let's make up a list - 'App', 'Inventor' and 'iOS'. According to this example, I would like to introduce some terms.

  • Length - the length of the list is 3 (it has 3 items in the list)

  • Index - the index of the item 'App' in the list is 1, because it is the first item in the list. Index 2 belongs with Inventor and iOS is 3.

To add a socket into the list block, click on the blue settings icon and drag the 'item' block into the mutator.

The make a list block provides sockets to plug in. The blocks you plug in will then be stored as a list.

This is how you create a list and temporarily place it.

Lists support strings, booleans, numbers, colors, new lists, dictionaries, components and helper blocks. Lists also support variables and procedures, only if the blocks are not in a definition (i.e. variables).

You can add items to a list by using the add items to list blocks. By clicking on the blue settings icon, you can add new items.

The is in list? block checks whether the 'thing' is stored in the 'list'.

The length of list block returns a number, indicating the length of the list, in other words, how many items the list consists.

The pick a random item block randomly selects an item in the list. If you use this block for an empty list, the app will report an error, as shown below.

The index in list block returns the index of the given item in the list, while the select list item block returns an item in the list prior to the given index.

You can use the remove item from list block to remove a list item.

The list from csv row block converts a text string to a list. Turning 1,2,3 to a list will result in a list having 1, 2 and 3 as its list items.

On the opposite, you can turn a list back into a string. The join items using separator block returns a text string that converts the list into text, with all items separated in the 'separator'.

With the append to list block, list2 will be combined with list1. All of the items in list2 will appear at the back of list1.

Why lists and/or variables?

Lists and variables are intended to make life easier for everyone. Without lists and variables, we will have to frequently store and delete data in databases, which confuses people and often times end up with bugs. Guess what, without lists and variables, you are unlikely to make an advanced application, probably just a toggle game.