How to | Create Lists
Lists are very important and general structures in the Wolfram Language. They allow you to 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:
{2, 3, 5, 6}Or use List, which automatically is changed to {}:
List[2, 3, 5, 6]Use Range with one argument to create a list of integers starting at 1:
Range[10]Or use Range with two arguments to create a list of integers starting higher:
Range[5, 10]With three arguments the offset can be different than 1:
Range[5, 10, 1 / 2]This squares each element of the list:
Range[10] ^ 2Or use Table to create the first 10 squares:
Table[i ^ 2, {i, 10}]Just like Range, Table can start higher or jump by any amount:
Table[i ^ 2, {i, 5, 10, 1 / 2}]Use NestList to create a list of the results of applying f to x for 0 through 3 times:
NestList[f, x, 3]Use Array to create a list of length 4, with elements f[i]:
Array[f, 4]Array[f, {3, 2}]Use List to create lists of strings:
List[{"a", "b", "c"}, {"you", "are", "good"}]A matrix in the Wolfram Language is a list of lists.
Use RandomInteger to create a 4×4 matrix of random integers between 0 and 10 (stored as m):
m = RandomInteger[10, {4, 4}]Use MatrixForm to see m as a 2D matrix:
MatrixForm[m]You can apply functions to a list.
You can directly apply math functions to a list:
Sqrt[{1, 2, 3, 4}]Math functions keep going deeper:
1 + {{a}, {a, b}, {a, b, c}} ^ 2Some functions give a number as a result:
Max[{1, 2, 3, 4}]Length gives the length of a list:
Length[{a, b, c}]Use Map to apply a function to the elements of a list (not needed for math functions):
Map[f, {a, b, c}]This uses Map to apply Length to each sublist:
Map[Length, {{a}, {a, b}, {a, b, c}}]Similarly, this finds the maximum of each sublist:
Map[Max, {{a}, {a, b}, {a, b, c}}]