Lists in App Inventor

Table of Contents

Basics of the List
Lists in a List
Searching a List and a List of Lists
Adding a List to a List
ForEach, Working through a List

Do you want to add to a list or replace a list, that is the question, because you can do both. 

The Basics of the List:

A App Inventor list can be a simple thing, as in '(a b c d e f)'  

Remember that a list must be declared as a list, normally with the statement 'Make A List'
as shown here.
You can destroy a list by assigning a string to it, as 'set MyList to text "abc" will change your list into a text and future List statements will no longer work until you make it a list again.
(a b c d e f) is what prints out when you put the above list into a Label. (a b c (d e f) is a list of 4 items, one of which is a list))

Lists in a List (List of Lists):

or it can be a list of lists, like this
Notes about Lists in a List:
Pick Random Item in List: will work with a list of lists and will return a sub-list, ((a a a)(b b b)(c c c)) will return one of the sub-lists (? ? ?)
while (a b c
(a a a)(b b b)(c c c)) will return one of the 6 items in the list

Is in List: Will only search a single list and will stop when it encounters a sub-list. (see Searching a List of Lists below)

Length of List: Will only return the number of items in the main list, ie
(a b c (a a a)(b b b)(c c c)) returns 6, 3 items and 3 sub-lists not 12.

Adding Lists

Sometimes you want to have a long list of static items in a list but you can only add something like 50 or so items before you run out of horizontal room

To fix this you can make 2 or more list and then add them together using the "APPEND TO LIST" statement.

Searching A List:

You can use the App Inventor Text block 'Contains' on both a list and a list of lists

But 'is in list' only checks the first list it finds in a list of list (same data as above)

Searching A List of Lists:

Here we add 3 lists to a list and now we what to search them.
First we will just show you the Raw List.
Then we will print out each Item in the List with a Space between them.
Now we will search through the List using the statement 'position in list', looking for "Item 2.3".
But it Fails, because 'position in list' will only search the List and not a list of List. If you have something like (a b c (d e f)) 'positon in list' will find a, b or c but nothing after that.
And searching for "d e f" or  "(d e f)" in "((a b c) (d e f) (g h i))" will also Fail.

OK so how do we search a list of lists.
It appears that Lists are a sub-class of the Text item (ie a specially formatted Text item is a List).
So just to find out if a string is in a List of Lists we can use the Text statement 'CONTAINS'. Now this just returns either True or False so we can use this first to see if an item is in the lists before we spend the time to search it in depth.

Use an 'if then do' statement to call 'contains' and if true then we use the 'foreach' statement to go through each list in the list and do a 'position in list' search on each subList.

Source File:

You can add a list to a list


*GOOGLE and Google App Inventor is a trademark of Google Inc.   All screenshots of 'Google App Inventor' are used in accordance with Google Inc Permission guidelines.
Edward Filkins,
Oct 3, 2010, 3:28 PM
Edward Filkins,
Dec 7, 2010, 12:22 PM