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 threedimensional 2×2×2 array. It is a list of lists of lists:
give a list of imax values of f
give a list of the values of f as i runs from 1 to imax
give a list of values with i running from imin to imax
use steps of di
generate a multidimensional table
give a list of the values of f as i successively takes the values i1, i2,
display a list in tabular form
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:
give the i th sublist in t (also input as ti)
give a list of the parts i through j
give a list of the i1 th, i2 th, parts of t
give the part of t corresponding to t[[i]][[j]]
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 twodimensional 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 list
the i th element of list (the first element is list[[1]])
a list of the i th, j th, elements of list
a list of the i th through j th elements of 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]].
extract the i th element of a list
reset the i th element of a list
Arraylike 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.
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 twocomponent 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.
build a lengthn vector by evaluating f with i=1,2,,n
build a lengthn vector of the form {a[1],a[2],}
create the list {1,2,3,,n}
create the list {n1,n1+1,,n2}
create the list {n1,n1+dn,,n2}
give the i th element in the vector list
give the number of elements in list
c v
multiply a vector by a scalar
dot product of two vectors
cross product of two vectors (also input as a×b )
Euclidean norm of a vector
Functions for vectors.
build an m×n matrix by evaluating f with i ranging from 1 to m and j ranging from 1 to n
build an m×n matrix with i,j th element a[i,j]
generate an n×n identity matrix
generate a square matrix with the elements in list on the main diagonal
give the i th row in the matrix list
give the j th column in the matrix list
give the i,j th element in the matrix list
give the dimensions of a matrix represented by list
Functions for matrices.
display the elements of list in a column
display list in matrix form
Formatting constructs for vectors and matrices.
This builds a 3×3 matrix with elements :
This displays s in standard twodimensional 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
dot product of two matrices
matrix inverse
n th power of a matrix
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 Mathematica" discusses many other matrix operations that are built into the Wolfram Language.
Getting Pieces of Lists
the first element in list
the last element
the n th element
the n th element from the end
elements m through n
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:
the first n elements in list
the last n elements
elements m through n (inclusive)
list with its first element dropped
list with its first n elements dropped
list with its last element dropped
list with its last n elements dropped
list with elements m through n dropped
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
the positions at which form occurs in list
the number of times form appears as an element of list
test whether form is an element of list
test whether form occurs nowhere in list
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
add element at the beginning of list
add element at the end of list
insert element at position i in list
insert at position i counting from the end of list
interleave element between the entries of list
delete the element at position i in list
replace the element at position i in list with new
replace list[[i,j]] with new
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.
assign a variable to be a list
assign a new value to the i th element
Resetting list elements.
This defines v to be a list:
This sets the third element to be x:
Now v has been changed:
replace the (i,j) th element of a matrix
replace the i th row
replace the i th column
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
concatenate lists together
combine lists, removing repeated elements and sorting the result
interleave elements of list1 and list2
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.
give a list of the distinct elements in the listi
give a list of the elements that are common to all the listi
give a list of the elements that are in universal, but not in any of the listi
give a list of all subsets of the elements in list
delete all duplicates from list
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
sort the elements of list into a standard order
sort elements, removing any duplicates
reverse the order of elements in list
rotate the elements of list n places to the left
rotate n places to the right
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:
pad list on the left with x to make it length len
pad list on the right
Padding lists.
This pads a list with x's to make it length 10:
Grouping and Combining Elements of Lists
partition list into nelement pieces
use offset d for successive pieces
split list into pieces consisting of runs of identical elements
split list into runs of elements with identical values when f is applied
gather the elements of list into sublists of identical elements
gather the elements of list into sublists with identical values when f is applied
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:
generate all possible ntuples of elements from list
generate all tuples whose i th element is from listi
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
sort the elements of list into order
the positions in list of the elements in Sort[list]
the first n elements of Ordering[list]
the last n elements of Ordering[list]
all possible orderings of list
the smallest element in list
the largest element in list
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.
flatten out all levels in list
flatten out the top n levels in list
partition into blocks of size n1×n2×
interchange the top two levels of lists
rotate successive levels by ni places
pad successive levels to be length ni
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".