A Series of Items

Lists are Mint's main compound data structure. Lists can hold values of any type, including other lists.
myList = [10, "bye", "hi", 14]                //A list of numbers and strings
otherList = [[0, 1], [1, 2, 3], [3, 4, 5]]    //A list of lists
To get item number X from a list, you index the list using square brackets.
list = [5, 16, 8, -2]
print list[0]     //The first item in the list.
print list[1]     //The second item in the list.
List slots are numbered starting from zero, so list[0] is the first item, and list[1] is the second. list[2] is the third.

You can also index lists backwards using negative numbers.
list = [5, 16, 8, -2]
print list[-1]     //The last item in the list.
print list[-2]     //The second to last item in the list.
You can check if a list contains certain items by using the 'in' operator.
print 2 in [1, 2, 3]    //true
print 5 in [0, 1, 0]    //false
Strings are very similar to lists, and therefore support the 'in' operator and indexing.
print "jo" in "joe"    //true
print "joe"[1]         //gets the second character of "joe"
To change the value of a list slot, use assignment.
myList = [0, 0, 0, 0]
myList[3] = 5000
print myList     //prints [0, 0, 0, 5000]
There is an interesting object known as the Ellipses which you can use in lists.
print ...
import type
print type(...)    //Prints the string "ellipsis".
lst = [1, 2, 4, ..., 128]
print lst[0]    //prints 1
print lst[3]    //prints the list [1, 2, 4, ..., 128]
Ellipses mean "refer to the list itself". That's why printing lst[3] prints the entire list. It is an easy way to put a list inside of itself.

Previous Lesson: More Control Flow
Next Lesson: Tables
Table of Contents