Wolfram: List. Create (Table)

By Xah Lee. Date: . Last updated: .

Table

Use Table to generate a flat list or nested rectangular array (aka n-dimensional matrix). Like Python list comprehension but more powerful.

💡 TIP: Table is the most powerful way to generate a list, and is most frequently used. Master it.

Generate Flat List

Table[expr, n]

a list with expr repeated n times.

Table[ x, {3}]
(* {x, x, x} *)
Table[expr, {i, max}]

a list, each item is expr with the variable i successively taking on the values 1 through max (in steps of 1).

Table[ x, {x, 3}]
(* {1, 2, 3} *)
Table[expr, {i, min, max}]

start with min.

Table[ x, {x, 6, 9}]
(* {6, 7, 8, 9} *)

Table[ 2 x + y , {x, 1, 4}]
(* {2 + y, 4 + y, 6 + y, 8 + y} *)
Table[expr, {i, min, max, step}]

with increment step.

Table[ x, {x, 1, 3, 1/2}]
(* {1, 3/2, 2, 5/2, 3} *)

Table[ x, {x, 1, 3, 0.5}]
(* {1., 1.5, 2., 2.5, 3.} *)
Table[expr, {i, ListOfValues}}]

a flat list. Each item use the given list of values ListOfValues.

Table[ x, {x, {3, 9, 2}}]
(* {3, 9, 2} *)

Example. Symbolic Expression

any part can be symbolic

Table[ Sin[x], {x, 0, 2 Pi, Pi/4}]
(* {0, 1/Sqrt[2], 1, 1/Sqrt[2], 0, -(1/Sqrt[2]), -1, -(1/Sqrt[2]), 0} *)

Example. Arbitrary Expression

(* example of arbitrary expression  *)
Table[ f[y] I Pi/x, {x, 3}]
(* {I*Pi*f[y], I/2*Pi*f[y], I/3*Pi*f[y]} *)

Generate Rectangular Array with Table

Table[expr, iterSpec1, iterSpec2, etc]

Generate a nested list, of rectangular array.

the iterSpec can take any of the following form:

  • {n} → repeat n times.
  • {var, max}
  • {var, min, max}
  • {var, min, max, step}
  • {var, listOfValues}
Table[ ff[x, y], {x, 3}, {y, 2}]
(* {
{ff[1, 1], ff[1, 2]},
{ff[2, 1], ff[2, 2]},
{ff[3, 1], ff[3, 2]}
} *)

Table[ x + y, {x, 3}, {y, 2}]
(* 
{
{2, 3},
{3, 4},
{4, 5}
} *)

Wolfram. List Operations, and Loop, Iteration, Recursion