# How to create a variable List

I need, given "n", create a list {a1,a2,...,an}. For example: Given n=3, the list that must be created is composed by the symbolic variables {a1,a2,a3}. Sorry if this question is simple. Does anybody knows how to do it? Thanks.

• Use e.g. Array[a, 3]. This will give you indexed variables which should serve the same purpose as those you look for. – MarcoB Jun 8 '18 at 18:00
• "a" <> ToString[#] & /@ Range[10] or Table["a" <> IntegerString[i, 10], {i, 1, 10}] – user1066 Jun 9 '18 at 0:01

For example, for n=5

Table[Symbol["a" <> ToString[i]], {i, 5}]


but consider that in any case it may not be optimal for what you need to do. If you want to share what you are planning to use this list for, we may help you with a better construct.

• Thanks very much Fraccalo. Helped quite a lot. I am trying to manipulate equations based on Feynman diagrams. But I won't bother you with this. You have just helped me a lot. Thank You again. Best wishes, J.R.Descardeci – Jose Ricardo Descardeci Jun 8 '18 at 18:17

You might also try with:

Table[Unique["a"], {n}]


The advantage and disadvantage of this is that generated variables are unique - there will be no conflict with but also no reuse of existing ones.

Note: depending on what you do, you might also want to Remove such generated variables after they are no longer needed, to avoid leaking memory for unused symbols.

As stated by MarcoB, indexed variables are highly convenient and less prone to error or difficulties. They can be displayed in any manner that you desire using Format.

n = 5;

list = Array[a, n];


To display as you show

Format[a[n_]] := Symbol["a" <> ToString[n]]

list


To display as subscripts

Format[a[n_]] := Subscript[a, n]

list


Or with a specified style

Format[a[n_]] := Style[Subscript[a, n], Bold, Italic, Blue]

list