# How to generically produce iterators (to loop over many variables)

A generalization of, for example:

Do[(* stuff *), {x, listx},{y, listy},{z, listz}]


I can almost do this with Table, but I need a way to flatten the outer brackets:

Table[{s, l[s]}, {s, {x, y, z}}]

(* out: {{x, l[x]},{y, l[y]},{z,l[z]}}


But to use this List, I need really to exchange the outerbrackets to simply a sequence.

If I append to the end

/.List->Sequence


It will remove all of the brackets and not give the desired result. Similar issues plague

Flatten
FlattenAt[ ,1]


etc. solutions.

Is there a good way to do this?

-
What is your desired result? because if Sequence[{x, l[x]},...] then you can just f @@ result and do not care about those very outer brackets. It is unclear to me what you are after. Table and Do are different in fundamental way. Do, unless forced, is not producing any output. If you need you can use it with Sow/Reap – Kuba Mar 31 '14 at 19:01
Do[Print[{x,y,z}],{x,Range[0.1,0.9,0.1]},{y,Range[0.1,0.9,0.1]},{z,Range[0.1,0.9‌​,0.1]}] – Steve Mar 31 '14 at 19:34
But I would like to be able to shorten this to something like:Do[Print[{x,y,z}],{x,y,z}@Range[0.1,0.9,0.1]] or whatever, some kind of easily generalizable way for more variables – Steve Mar 31 '14 at 19:36

If I understand your query, you're looking for something like:

myNestedLoop[range_, vars_] :=
Sequence @@ Transpose[{vars, ConstantArray[range, Length@vars]}]

Table[{a, b, c}, Evaluate@myNestedLoop[Range[3, 9, 3], {a, b, c}]]

(*
{{{{3, 3, 3}, {3, 3, 6}, {3, 3, 9}}, {{3, 6, 3}, {3, 6, 6}, {3, 6,
9}}, {{3, 9, 3}, {3, 9, 6}, {3, 9, 9}}}, {{{6, 3, 3}, {6, 3,
6}, {6, 3, 9}}, {{6, 6, 3}, {6, 6, 6}, {6, 6, 9}}, {{6, 9, 3}, {6,
9, 6}, {6, 9, 9}}}, {{{9, 3, 3}, {9, 3, 6}, {9, 3, 9}}, {{9, 6,
3}, {9, 6, 6}, {9, 6, 9}}, {{9, 9, 3}, {9, 9, 6}, {9, 9, 9}}}}
*)


You can stack as needed:

Table[{a, b, c, d}, Evaluate@myNestedLoop[Range[2, 4, 2], {a, b}],
Evaluate@myNestedLoop[Range[3, 5, 2], {c, d}]]

(*
{{{{{2, 2, 3, 3}, {2, 2, 3, 5}}, {{2, 2, 5, 3}, {2, 2, 5, 5}}}, {{{2,
4, 3, 3}, {2, 4, 3, 5}}, {{2, 4, 5, 3}, {2, 4, 5, 5}}}}, {{{{4,
2, 3, 3}, {4, 2, 3, 5}}, {{4, 2, 5, 3}, {4, 2, 5, 5}}}, {{{4, 4,
3, 3}, {4, 4, 3, 5}}, {{4, 4, 5, 3}, {4, 4, 5, 5}}}}}
*)


Arguments can be lists created elsewhere of course.

As alluded to in comments, not sure of the utility of such a construct (I suppose if one had a gazillion loop variables with large common sets of bounds it saves some typing), perhaps some more concrete examples of your desired end-result might elicit a better way to accomplish it overall...

-
I appreciate your effort, and yeah, I guess it's just an attempt to save some typing. In the past, I've often noticed that when I find myself typing the same thing over and over, there is usually a more elegant way to formulate it. But writing ", {a, Range[d, 1 - d, d]}, {t, Range[d, 1 - d, d]}, {g, Range[d, 1 - d, d]}, {v, Range[d, 1 - d, d]}, {V, Range[d, 1 - d, d]}" isn't really that bad, I guess – Steve Apr 1 '14 at 18:33