Next: Dictionaries
Prev: Names
C-- using a list construction expression.
A list construction expression has the following syntax:
[expression1, expression2, ...]expression1, expression2, ... are the elements of the list. You can also splice a list into a list construction expression; see Splicing. The result of a list construction expression is a list containing the values of the expressions. Here are some examples of list construction expressions:
[1, 2, 3] [a + 5, 'string, [3, "b"], tostr(~none)] []To retrieve an element of a list, you can use a list selection expression, which has the following syntax:
list[element]list must be an expression of list type (or string type; see below) and element is an expression of integer type whose value is between one and the length of the list. The value of a list selection expression is the value of the element of list numbered by element, where the elements of list are numbered starting from one. For example, the following method returns
3:
var a;You can use the list selection expression with strings as well as with lists. In this case,a = ["foo", 2, 'bar, 3, ~parents]; return a[4];
C-- treats the string as if it were a list
of one-character strings. The following method returns "p":
var a;You can also use the list selection expression with dictionaries; we will describe this usage in the next section.a = "Depth"; return a[3];
You can look for a piece of data in a list, you can use a list search expression, which has the following syntax:
element in listThe value of this expression is the index of the first occurrance of element in list, or
0 if element does not occur
in list. The following method returns [3, 0]:
var a;As with the list selection expression, you can use the list search expression with strings as well as with lists. In this case, both element and list must be strings, and the result is the first position at which element occurs in list, or 0 if it does not occur.a = ['foo, "bar", 4 + 2, #77]; return [6 in a, 'quux in a];