# How to insert a list in the second argument of Table?

I wrote the following table:

k = 4;
T = Table[{a[n], 0, 1}, {n, 1, k}]


Which generates:

{{a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}}


Now in the I want to use this in the next table:

Table[Table[a[n], {n, 1, 4}], x]


In a way that makes Mathematica compute:

Table[Table[a[n], {n, 1, 4}], {a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}]


Which works correctly. I tried to work with Sequence and Apply just as this question and I also tried Splice but it doesn't work. Perhaps it work in some way with these functions but I don't know how to do it.

• What is the x in the second command? Could you, please, elaborate?
– bmf
Commented Sep 13, 2022 at 20:29
• @bmf x should be {a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}. But all I have is the list {{a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}}. Commented Sep 13, 2022 at 20:34
• For members looking for a similar question maybe the title should be "How to insert a list in the second argument of Table" or "Using a list as a table iterator" or Commented Sep 13, 2022 at 20:39
• @RedBanana thanks for the clarification :)
– bmf
Commented Sep 13, 2022 at 20:41
• Does this answer your question? How to programmatically specify multiple iterators? Commented Sep 13, 2022 at 22:57

\$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global*"]

k = 4;
T = Table[{a[n], 0, 1}, {n, 1, k}]

(* {{a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}} *)

Attributes[Table]



Since Table has the attribute HoldAll you need Evaluate

tab=Table[Table[a[n], {n, 1, 4}], Evaluate[Sequence @@ T]]

(* {{{{{0, 0, 0, 0}, {0, 0, 0, 1}}, {{0, 0, 1, 0}, {0, 0, 1, 1}}}, {{{0, 1, 0,
0}, {0, 1, 0, 1}}, {{0, 1, 1, 0}, {0, 1, 1, 1}}}}, {{{{1, 0, 0, 0}, {1,
0, 0, 1}}, {{1, 0, 1, 0}, {1, 0, 1, 1}}}, {{{1, 1, 0, 0}, {1, 1, 0,
1}}, {{1, 1, 1, 0}, {1, 1, 1, 1}}}}} *)


Alternatively, Apply the Table

tab === Table @@ {Table[a[n], {n, 1, 4}], Sequence @@ T}

(* True *)

x={{a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}}

Table[Table[a[n], {n, 1, 4}], Evaluate[Sequence@@x]]==Table[Table[a[n], {n, 1, 4}], {a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}]

True


In the documentation for Table, in section "properties & relations" there is a subsection called "programmatic table construction" where one can find other ways to use a list to specify bounds for Table. For example :

Table[Table[a[n], {n, 1, 4}], ## ]&@@x==Table[Table[a[n], {n, 1, 4}], {a[1], 0, 1}, {a[2], 0, 1}, {a[3], 0, 1}, {a[4], 0, 1}]

True


Edit:

### How to use the errors to notice that x was not being evaluated

The error messages received might have been helpful as they showed that the list x was not being evaluated. Consider the following experiment:

x={i,4}

Table[i,x]


Error : Non-list iterator x at position 2 does not evaluate to a real numeric
value

Notice that the error says x and not the value of x which is {i,4} which might mean that x was not evaluated before the error happened. In that case we could try something even worse like

x=i


which we know leads to an error if we tried

Table[i,i]


Error : Non-list iterator i at position 2 does not evaluate to a real numeric
value.

But the error we receive if we use

Table[i,x]


is the same as the one before which mentions x instead of i.

Table[i,Evaluate[x]]


Gives the same error as Table[i,i] and so refers to i instead of x and as expected Table[i,{i,4}] and

x={i,4};Table[i,Evaluate[x]]


works.

To summarize, as the error did not mention x it was a signal that Table might not be seeing how x is defined. In fact to highlight that the second argument was not evaluated before the error was given one can also try :

x={i,4};Table[i,x+1-1]


which shows :

Error : Non-list iterator x+1-1 at position 2 does not evaluate to a real
numeric value

### Why doesn't Mathematica just evaluate the second argument ?

An experiment :

b=5;Table[b,{b,3}]


Does not give any errors. Why ? after all b=5 why is the output not the same as

Table[5,{5,3}]


Error : Raw object 5 cannot be used as an iterator

Well because b is not evaluated in Table as it holds its arguments. If it did not then one would have to think of all the symbols already used each time Table is called. In that case, what happens if we use Evaluate ? Would it create an error instead of solving one ?

a=5;Table[a,Evaluate[{a,3}]]


Error: Raw object 5 cannot be used as an iterator.

Output: Table[a, {5, 3}]

a=5;Table[Evaluate[a],{a,3}]
`

Guess the output

Summary: It is crucial that Table does not know how the variables are defined in the notebook until it is safe to do so, in particular until it has found the iterator (the variable that changes at each iteration).

Possible exercise: How would one go about making a homemade Table that computes table[g[h,u],{h,8}] in the light of the discussion above ?