Wolfram Language Fast Introduction for Programmers
Get Started »

Lists

Lists are indicated in the Wolfram Language by { ... }
They can contain any kinds of expressions:

X

In the Wolfram Language, lists represent grouped data. They can have any structure and size, and the language automatically infers the most efficient internal representation. Java programmers must decide between either primitive arrays or classes from the Collection Framework, and this choice is heavily dependent on context.

Parts of lists are indexed starting at 1, and can be extracted using [[ ... ]]

In[1]:=
X
Out[1]=

Negative indices count from the end:

In[2]:=
X
Out[2]=

Like Java arrays, Wolfram Language lists are indicated by curly braces {...}. Indexing in the Wolfram Language uses [[...]] rather than [...], and indices start at 1 instead of 0.

Wolfram Language lists are indicated by curly braces {...}, while Python arrays use square brackets [...]. Indexing in the Wolfram Language uses [[...]] rather than [...], and indices start at at 1 instead of 0.

Many operations immediately thread over lists:

In[1]:=
X
Out[1]=
In[2]:=
X
Out[2]=

Threading operations over Java arrays or collections would require loop structures.

In the Wolfram Language, + is strictly for arithmetic addition. String concatenation, for example, uses <>.

Refer to spans in lists using ;;

In[1]:=
X
Out[1]=

Java collections and arrays do not have anything comparable to Wolfram Language spans for extracting subsets.

;; spans in the Wolfram Language are similar to the Python : slices, though they generalize to multidimensional arrays.

QUICK REFERENCE: Operations on Lists »

Check Your Understanding

Which of the following will get the third element of the list {x, y, z}?

{x, y, z}[3]

Incorrect. Extracting an indexed part of a list requires double brackets.

{x, y, z}[[2]]

Incorrect. Indices start at 1 in the Wolfram Language, not 0, so the third index is 3.

{x, y, z}[[3]]

Correct.

Which of the following evaluates to 3?

{2, 4, 5, 7}[[3]]

Incorrect. The 3 refers to the third part of the list, which is 5.

({2, 3, 5, 7} + 1)[[2]]

Incorrect. Addition is threaded over lists, so after adding 1, the second element is 4.

{2, 3, 5, 7}[[2 ;; 4]][[1]]

Correct. The span takes the list's second through fourth terms, so the span's first element is 3.

Which of the following will get the element a from the list {a, b, c}?

{a, b, c}[1]

Incorrect. Extracting an indexed part of a list requires double brackets.

{a, b, c}[[0]]

Incorrect. In the Wolfram Language, indices start at 1, not 0.

{a, b, c}[[1]]

Correct.