2
$\begingroup$

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$
2
  • 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
    Commented Jul 1, 2018 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
    Commented Jul 1, 2018 at 17:22

0

Browse other questions tagged or ask your own question.