Reading from a List
-
Prerequisite
Description
A list is just like many variable items positioned sequentially. You can read the value of any item using its position, or get information about the list itself.
Getting the Length of a List
This reporter block tells you how many items are in the list.
Demo
Note that the position indices of the items go from 1 to the list length. For example, if you want to insert an item before the last item of a list, you can use the list length as the position.
Reading an Item’s Value at a Position
If you know the position of an item, you can read its value using this block:
The position index has to be at least 1 and at most the length of the list. For example, to get the last item’s value, you can do this:
Searching for an Item by Its Value
You can not only get an item by its position, but also find the position of an item if you know its value.
If the value is not found, then the reported value will be 0:Whether a Value is In the List
You can find out if a value is contained in a list using this boolean block, which will report “true” or “false”.
Searching for a part of an item
Sometimes you want to find any item that contains some value. In other words, you are searching for a part of an item. You can use this block:
For example, in the list of student names, you might want to find the index of the first item that contains “Mike” like this:
-