1
$\begingroup$

This question already has an answer here:

How can I generate a list of variable-names like {p0, p1, p2, p3} etc...

I tried:

mylist = p # &@Range[1, 4]

But then the result is:

{p, 2 p, 3 p, 4 p}

$\endgroup$

marked as duplicate by Lukas Lang, Coolwater, m_goldberg, MarcoB, halirutan Jul 1 '18 at 20:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ you could do something like ToExpression[StringJoin["p",ToString[#]]]&/@Range[4] but in general it is not recommended; the usual recommendation is to use indexed variables ie use p/@Range[4]; this seems relevant; on a different note, perhaps this is helpful, also $\endgroup$ – user42582 Jul 1 '18 at 15:30
  • 1
    $\begingroup$ You could use Table[StringTemplate["p``"][i], {i, 5}] (see this answer) or maybe Table["p" <> IntegerString[i, 10], {i, 1, 5}]. But, as user42582 points out, it is better to consider using indexed variables. For example: Array[p, 5] (but avoid subscripted variable names, as in Array[Subscript[a, #] &, {5}], as (IMO at least) these can cause problems). $\endgroup$ – user1066 Jul 1 '18 at 17:22