Lists
Lists
Making Lists of Objects
In doing calculations, it is often convenient to collect together several objects, and treat them as a single entity. Lists give you a way to make collections of objects in the Wolfram Language. As you will see later, lists are very important and general structures in the Wolfram Language.
A list such as {3,5,1} is a collection of three objects. But in many ways, you can treat the whole list as a single object. You can, for example, do arithmetic on the whole list at once, or assign the whole list to be the value of a variable.
Here is a list of three numbers:
This squares each number in the list, and adds 1 to it:
This takes differences between corresponding elements in the two lists. The lists must be the same length:
The value of % is the whole list:
You can apply any of the mathematical functions in "Some Mathematical Functions" to whole lists:
Just as you can set variables to be numbers, so also you can set them to be lists.
This assigns v to be a list:
Wherever v appears, it is replaced by the list:
Collecting Objects Together
We first encountered lists in "Making Lists of Objects" as a way of collecting numbers together. Here, we shall see many different ways to use lists. You will find that lists are some of the most flexible and powerful objects in the Wolfram Language. You will see that lists in the Wolfram Language represent generalizations of several standard concepts in mathematics and computer science.
At a basic level, what a Wolfram Language list essentially does is to provide a way for you to collect together several expressions of any kind.
Here is a list of numbers:
This gives a list of symbolic expressions:
You can differentiate these expressions:
And then you can find values when x is replaced with 3:
The mathematical functions that are built into the Wolfram Language are mostly set up to be "listable" so that they act separately on each element of a list. This is, however, not true of all functions in the Wolfram Language. Unless you set it up specially, a new function f that you introduce will treat lists just as single objects. "Applying Functions to Parts of Expressions" and "Structural Operations" will describe how you can use Map and Thread to apply a function like this separately to each element in a list.
Making Tables of Values
You can use lists as tables of values. You can generate the tables, for example, by evaluating an expression for a sequence of different parameter values.
This gives a table of the values of , with running from 1 to 6:
Here is a table of for from to :
This gives the numerical values:
You can also make tables of formulas:
Table uses exactly the same iterator notation as the functions Sum and Product , which are discussed in "Sums and Products":
This makes a table with values of x running from 0 to 1 in steps of 0.25:
You can perform other operations on the lists you get from Table :
TableForm displays lists in a "tabular" format. Notice that both words in the name TableForm begin with capital letters:
All the examples so far have been of tables obtained by varying a single parameter. You can also make tables that involve several parameters. These multidimensional tables are specified using the standard Wolfram Language iterator notation, discussed in "Sums and Products".
This makes a table of with running from 1 to 3 and running from 1 to 2:
The table in this example is a list of lists. The elements of the outer list correspond to successive values of . The elements of each inner list correspond to successive values of , with fixed.
Sometimes you may want to generate a table by evaluating a particular expression many times, without incrementing any variables.
This creates a list containing four copies of the symbol x:
This gives a list of four pairs of numbers sampled from {1,2,3,4}. Table reevaluates RandomSample [{1,2,3,4},2] for each element in the list, so that you get four different samples:
This evaluates for each of the values of i in the list {1,4,9,16}:
This creates a 3×2 table:
In this table, the length of the rows depends on the more slowly varying iterator variable, i:
You can use Table to generate arrays with any number of dimensions.
This generates a three‐dimensional 2×2×2 array. It is a list of lists of lists:
Functions for generating tables.
You can use the operations discussed in "Manipulating Elements of Lists" to extract elements of the table.
This creates a table and gives it the name sq:
This gives the third part of the table:
This gives a list of the third through fifth parts:
This creates a 2×2 table, and gives it the name m:
This extracts the first sublist from the list of lists that makes up the table:
This extracts the second element of that sublist:
This does the two operations together:
This displays m in a "tabular" form:
Ways to extract parts of tables.
As mentioned in "Manipulating Elements of Lists", you can think of lists in the Wolfram Language as being analogous to "arrays". Lists of lists are then like two‐dimensional arrays. When you lay them out in a tabular form, the two indices of each element are like its and coordinates.
Manipulating Elements of Lists
Many of the most powerful list manipulation operations in the Wolfram Language treat whole lists as single objects. Sometimes, however, you need to pick out or set individual elements in a list.
You can refer to an element of a Wolfram Language list by giving its "index". The elements are numbered in order, starting at 1.
{a,b,c}
a list
Operations on list elements.
This extracts the second element of the list:
This extracts a list of elements:
This assigns the value of v to be a list:
You can extract elements of v:
By assigning a variable to be a list, you can use Wolfram Language lists much like "arrays" in other computer languages. Thus, for example, you can reset an element of a list by assigning a value to v[[i]].
Array‐like operations on lists.
Here is a list:
This resets the third element of the list:
Now the list assigned to v has been modified:
Vectors and Matrices
Vectors and matrices in the Wolfram Language are simply represented by lists and by lists of lists, respectively.
{a,b,c}
vector
{{a,b},{c,d}}
matrix
The representation of vectors and matrices by lists.
This is a 2×2 matrix:
Here is the first row:
Here is the element :
This is a two‐component vector:
The objects p and q are treated as scalars:
Vectors are added component by component:
This gives the dot (scalar) product of two vectors:
You can also multiply a matrix by a vector:
Or a matrix by a matrix:
Or a vector by a matrix:
This combination makes a scalar:
Because of the way the Wolfram Language uses lists to represent vectors and matrices, you never have to distinguish between "row" and "column" vectors.
c v
multiply a vector by a scalar
a.b
dot product of two vectors
Functions for vectors.
Table [f,{i,m},{j,n}]
build an m×n matrix by evaluating f with i ranging from 1 to m and j ranging from 1 to n
Functions for matrices.
Formatting constructs for vectors and matrices.
This builds a 3×3 matrix with elements :
This displays s in standard two‐dimensional matrix format:
This gives a vector with symbolic elements. You can use this in deriving general formulas that are valid with any choice of vector components:
This gives a 3×2 matrix with symbolic elements. "Building Lists from Functions" discusses how you can produce other kinds of elements with Array :
Here are the dimensions of the matrix on the previous line:
This generates a 3×3 diagonal matrix:
c m
multiply a matrix by a scalar
a.b
dot product of two matrices
Some mathematical operations on matrices.
Here is the 2×2 matrix of symbolic variables that was defined:
This gives its determinant:
Here is the transpose of m:
This gives the inverse of m in symbolic form:
Here is a 3×3 rational matrix:
This gives its inverse:
Taking the dot product of the inverse with the original matrix gives the identity matrix:
Here is a 3×3 matrix:
Eigenvalues gives the eigenvalues of the matrix:
This gives a numerical approximation to the matrix:
Here are numerical approximations to the eigenvalues:
"Linear Algebra in Wolfram Language" discusses many other matrix operations that are built into the Wolfram Language.
Getting Pieces of Lists
the list of elements at positions n1, n2, …
Picking out elements of lists.
We will use this list for the examples:
Here is the last element of t:
This gives the third element:
This gives the list of elements 3 through 6:
This gives a list of the first and fourth elements:
Picking out sequences in lists.
This gives the first three elements of the list t defined above:
This gives the last three elements:
This gives elements 2 through 5 inclusive:
This gives elements 3 through 7 in steps of 2:
This gives t with the first element dropped:
This gives t with its first three elements dropped:
This gives t with only its third element dropped:
"Manipulating Expressions like Lists" shows how all the functions here can be generalized to work not only on lists, but on any Wolfram Language expressions.
The functions here allow you to pick out pieces that occur at particular positions in lists. "Finding Expressions That Match a Pattern" shows how you can use functions like Select and Cases to pick out elements of lists based not on their positions, but instead on their properties.
Testing and Searching List Elements
Testing and searching for elements of lists.
"Getting Pieces of Lists" discusses how to extract pieces of lists based on their positions or indices. The Wolfram System also has functions that search and test for elements of lists, based on the values of those elements.
This gives a list of the positions at which a appears in the list:
Count counts the number of occurrences of a:
This shows that a is an element of {a,b,c}:
On the other hand, d is not:
This assigns m to be the 3×3 identity matrix:
This shows that 0 does occur somewhere in m:
This gives a list of the positions at which 0 occurs in m:
As discussed in "Finding Expressions That Match a Pattern", the functions Count and Position , as well as MemberQ and FreeQ , can be used not only to search for particular list elements, but also to search for classes of elements which match specific "patterns".
Adding, Removing, and Modifying List Elements
Functions for manipulating elements in explicit lists.
This gives a list with x prepended:
This inserts x so that it becomes element number 2:
This interleaves x between the entries of the list:
This replaces the third element in the list with x:
This replaces the 1, 2 element in a 2×2 matrix:
Functions like ReplacePart take explicit lists and give you new lists. Sometimes, however, you may want to modify a list "in place", without explicitly generating a new list.
v={e1,e2,…}
assign a variable to be a list
v[[i]]=new
assign a new value to the ith element
Resetting list elements.
This defines v to be a list:
This sets the third element to be x:
Now v has been changed:
m[[i,j]]=new
replace the (i,j)th element of a matrix
m[[i]]=new
replace the ith row
Resetting pieces of matrices.
This defines m to be a matrix:
This sets the first column of the matrix:
This sets every element in the first column to be 0:
Combining Lists
Functions for combining lists.
Join concatenates any number of lists together:
Union combines lists, keeping only distinct elements:
Riffle combines lists by interleaving their elements:
Lists as Sets
The Wolfram Language usually keeps the elements of a list in exactly the order you originally entered them. If you want to treat a Wolfram Language list like a mathematical set, however, you may want to ignore the order of elements in the list.
Complement [universal,list1,…]
give a list of the elements that are in universal, but not in any of the listi
Set theoretical functions.
Union gives the elements that occur in any of the lists:
Intersection gives only elements that occur in all the lists:
Complement gives elements that occur in the first list, but not in any of the others:
This gives all the subsets of the list:
DeleteDuplicates deletes all duplicate elements from the list:
Rearranging Lists
Functions for rearranging lists.
This sorts the elements of a list into a standard order. In simple cases like this, the order is alphabetical or numerical:
This sorts the elements, removing any duplicates:
This rotates ("shifts") the elements in the list two places to the left:
You can rotate to the right by giving a negative displacement, or by using RotateRight :
Padding lists.
This pads a list with x's to make it length 10:
Grouping and Combining Elements of Lists
Functions for grouping together elements of lists.
Here is a list:
This groups the elements of the list in pairs, throwing away the single element left at the end:
This groups elements in triples. There is no overlap between the triples:
This makes triples of elements, with each successive triple offset by just one element:
This splits up the list into runs of identical elements:
Here is a random list of integers:
This splits the list into runs of even and odd numbers:
Gather the integers into sublists:
Gather the integers into sublists based on equivalence mod 3:
Finding possible tuples of elements in lists.
This gives all possible ways of picking two elements out of the list:
This gives all possible ways of picking one element from each list:
Ordering in Lists
Ordering in lists.
Here is a list of numbers:
This gives the elements of t in sorted order:
This gives the positions of the elements of t, from the position of the smallest to that of the largest:
This is the same as Sort [t]:
This gives the smallest element in the list:
Rearranging Nested Lists
You will encounter nested lists if you use matrices or generate multidimensional arrays and tables. The Wolfram Language provides many functions for handling such lists.
A few functions for rearranging nested lists.
This "flattens out" sublists. You can think of it as effectively just removing all inner braces:
This flattens out only one level of sublists:
There are many other operations you can perform on nested lists. More operations are discussed in "Manipulating Lists".