Lists are very important and general structures in
Mathematica. They allow you treat collections of all kinds of objects as a single entity. There are many ways to construct them.
Use the shorthand notation {} to make a list:
| Out[1]= |  |
Or use
List, which automatically is changed to {}:
| Out[2]= |  |
Use
Range with one argument to create a list of integers starting at 1:
| Out[3]= |  |
Or use
Range with two arguments to create a list of integers starting higher:
| Out[4]= |  |
With three arguments the offset can be different than 1:
| Out[5]= |  |
This squares each element of the list:
| Out[6]= |  |
Or use
Table to create the first 10 squares:
| Out[7]= |  |
Just like
Range,
Table can start higher or jump by any amount:
| Out[8]= |  |
Use
NestList to create a list of the results of applying
f to
x for 0 through 3 times:
| Out[9]= |  |
Use
Array to create a list of length 4, with elements
f[i]:
| Out[10]= |  |
| Out[11]= |  |
Use
List to create lists of strings:
| Out[12]= |  |
A matrix in
Mathematica is a list of lists.
Use
RandomInteger to create a 4×4 matrix of random integers between 0 and 10 (stored as
m):
| Out[13]= |  |
Use
MatrixForm to see
m as a 2D matrix:
Out[14]//MatrixForm= |
| |  |
You can apply functions to a list.
You can directly apply math functions to a list:
| Out[15]= |  |
Math functions keep going deeper:
| Out[16]= |  |
Some functions give a number as a result:
| Out[17]= |  |
Length gives the length of a list:
| Out[18]= |  |
Use
Map to apply a function to the elements of a list (not needed for math functions):
| Out[19]= |  |
This uses
Map to apply
Length to each sublist:
| Out[20]= |  |
Similarly, this finds the maximum of each sublist:
| Out[21]= |  |