Elm ([[ ]])
Elm ([[ ]])
list[[ i ]] returns the element at position i of a list or expression.
Usages
list[[ i ]] or Elm[ list, i ]
list[[ i ]] returns the element at position i (starting at 1). The index can be a script variable.
| Example | in | out |
|---|---|---|
| The second element. | L := {a, b, c, d} |
r = b |
| The index is a script variable. | L := {a, b, c} |
r = b |
The same with Elm. |
Elm[ {a, b, c}, 2 ] |
b |
list[[ {i1, i2, ...} ]]
With a list of positions, the result is a list of the elements at those positions.
| Example | in | out |
|---|---|---|
| The second and fourth element. | L := {a, b, c, d} |
r = {b, d} |
| A list with one position gives a list. | L := {a, b, c, d} |
r = {a} |
list[[ i, j, ... ]]
With more indices, list[[ i, j ]] takes element j of element i, for example in a list of lists.
| Example | in | out |
|---|---|---|
Row 2, column 1. |
M := { {1, 2}, {3, 4} } |
r = 3 |
| The same in two steps. | M := { {1, 2}, {3, 4} } |
r = 3 |
expr[[ i ]] for other expressions
Indexing also works on other expressions: it returns the i-th argument.
| Example | in | out |
|---|---|---|
| The second term of a sum. | p := 2x^2 + 3x + 6 |
r = 3x |
| The exponent of a power. | q := x^2 |
r = 2 |
| The value of a quantity. | Quantity[3, 'm'][[1]] |
3 |
Details
| Invalid input | Result |
|---|---|
L := {a, b, c} |
Error: index out of bounds |
L := {a, b, c} |
Error: index out of bounds |