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.

Table

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

Table[expr, n]

a list with expr repeated n times.

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

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

Table[ x, {x, 3}]
(* {1, 2, 3} *)
Table[expr, {i, iMin, iMax}]

start with iMin.

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, iMin, iMax, di}]

with increment di.

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} *)

Generating Nested List with Table

Table[expr, iterSpec1, iterSpec2, etc]

a nested list. For each i, loop over the variable in each iterSpec.

Table[expr, iterSpec1, iterSpec2]

is same as

Table[Table[expr, iterSpec2], iterSpec1]

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, z], {x, 2}, {y, 2}, {z, 2}]
(*
{
 {
  {ff[1, 1, 1], ff[1, 1, 2]},
  {ff[1, 2, 1], ff[1, 2, 2]}},
 {
  {ff[2, 1, 1], ff[2, 1, 2]},
  {ff[2, 2, 1], ff[2, 2, 2]}}}
*)

Examples

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} *)

generating a 3D matrix.

WolframLang 3d matrix 2022-06-03 s3BYV
WolframLang 3d matrix

arbitrary expression

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

WolframLang Loop, Iteration, Recursion

WolframLang List Operations