# Introduction to Mathematica: Lists

At its most basic, a list is simply a sequence of Mathematica™ objects, separated by commas, and enclosed in curly braces { and }. This basic description, however, is as misleading as suggesting that a novel is simply a sequence of letters and punctuation marks. Lists are the primary structure that Mathematica™ uses to handle multiple objects at once, and have a flexibilty that is unheard-of in most other programming languages.

### Accessing Elements of Lists

Elements of lists are accessing using double square brackets. An integer greater than zero refers to that element in the list. A negative integer refers to the element in the list counting from the end. A pair of integers separated by double semicolons refers to the range of elements from the first index to the second index. Adding a second double semicolon and another integer specifies that elements will be skipped (e.g., list[[1;;5;;2]] means to take the first through fifth elements, counting every two elements, so positions 1, 3, and 5).

### Mathematical Operations on Lists

Standard mathematical operations apply to a list on an element-by-element basis. For exampling, adding a constant or multiplying by a constant means performing that operation on each element in the list individually. Applying a binary operation (an operation that applies to two arguments, such as addition or multiplication) to two lists, as long as they are the same length, applies that operation in a pairwise fashion. Some examples are illustrated to the right.

### List-Specific Functions

There are an enormous number of built-in functions for querying properties of lists or modifying lists. Several (hopefully self-explanatory) examples that we will use on this website are shown to the right.

### Combining Lists

The two most important ways we will be combining lists are Join (which appends the lists to each other in order) and Union (which collects all of the unique elements of a set of lists, including each unique element only once).

## Allowed List Elements

List elements can be, frankly, anything. Numbers. Variables. Functions. Plots. Operators. Text strings. Rules. Equations. Inequalities. Even other lists. Anything you can express in Mathematica™ can be an element of a list.

### Nested Lists

The last example from the previous section is an example of a nested list (i.e., a list of lists). This kind of structure is so powerful and so common in Mathematica™ that it is worth further examination. That particular example is a two-dimensional list. The outer list has length 7, and each inner list has length 2.

The inner lists are not restricted to all having the same dimensions, nor do they even have to be of the same type of information, but when they are of the same length and of the same type of information, there are a number of special features we can take advantage of. For example, a two-dimensional list, which in other computer languages is typically thought of as an “array,” can be easily displayed as a table, a grid, or a matrix, as shown below. This particular example can be thought of as a set of ordered-pairs (i.e., a set of (x,y) values) and thus could be plotted. This is a common structure for experimental data.

Most standard mathematical matrix operations are, not surprisingly, built-in to Mathematica™. One of the most commonly-used for our purposes is transposing the matrix (i.e., swapping rows for columns). This can be done with the function Transpose, or with a post-fix operator that looks like a superscript T, but is entered by the key-sequence <Esc>tr<Esc> (if you put a true superscript T instead, Mathematica™ will treat it as raising something to the power T). This is very frequently used to extract, for example, all the x values in a set of ordered pairs. Alternately, you can make a list of x values, then separately make a list of y values, and combine them using a transpose statement to generate a list of ordered pairs.

Nested lists are not limited to being only two levels deep. In fact, for our purposes, there is no practical limit to how deeply one can nest lists. We likely will not go much deeper than three or four levels deep in the materials presented on this website.

### Ways to Automatically Generate Lists

The simplest automatic way to generate a list is with the Range command, which takes up to three arguments. If a single argument is provided, Range starts at one and counts up by one until the value of that argument is reached. If two arguments are provided, Range starts at the first and counts by one up to the second. If a third argument is provided, Range starts at the first and counts by the third up to the second.

Perhaps the most flexible method of generating a list is the Table function. Table, in its most basic form, is Table[repeatUnit, {iterator with range}]. The examples to the right show how Table can be used to generate exactly the same results that Range did above, illustrating how the “iterator with range” works.

Table has much more flexibility, however, because the first argument is not limited to being the iterator by itself. It can be pretty much anything. Let’s look at several examples, including the results of calculations, lists, functions, and plots.

Table can also take multiple iterators. Below is an example with three iterators. This generates a three dimensional list of ordered triplets; so since the ordered triplet is a list itself, the result is a four-dimensional list. Notice that the first element of this structure is the portion where the first iterator takes on its first value.

In some cases, you may want to restructure this kind of table. So perhaps you are trying to generate a list of ordered triplets, (x,y,z) values, for plotting in three dimensions. In such a case you want, instead of the deeply-nested structure above, something like {{x1,y1,z1},{x2,y2,z2},…}. That specific example is most easily performed using the Flatten function with the argument “2” to indicate that the list should be flattened to level 2, as shown below. The range of different ways to restructure lists is quite wide, however, and is beyond the scope of this tutorial. I recommend looking into the documentation center under the heading Rearranging & Restructuring Lists, if you want more information.