# Numbering the elements of a list

From a vector q={Gamma,x,y}, I would like to create the following corresponding list : qn={1,2,3}.

Other example, if I have q={Gamma,x,y,theta}, I would like to retrieve qn={1,2,3,4}.

Is there a better way than Table[i, {i, 1, Length[q]}]?

Thank you

• Range[Length[q]] Nov 12 '15 at 10:26
• @JasonB probably you should post it as an answer. Nov 12 '15 at 10:38
• Right! For me, it is perfect. Thank you Nov 12 '15 at 10:39
• What would you like to get for {Gamma, x, x, Gamma}?
– Kuba
Nov 12 '15 at 10:44

This will give what you are looking for

q = {Gamma, x, y, theta};
Range[Length[q]]
(* {1, 2, 3, 4} *)


Or, with less typing,

Range@Length@q
(* {1, 2, 3, 4} *)


As @Jason already commented, the usual form would be

Range @ Length @ q;


If your goal is to number the list you can do

> Transpose[{Range @ Length @ q, q}]


{{1, Gamma}, {2, x}, {3, y}, {4, theta}}

or use MapIndexed

MapIndexed[List, q]


{{Gamma, {1}}, {x, {2}}, {y, {3}}, {theta, {4}}}

• Or, do MapIndexed[Reverse@*Flatten@*List, q] to get it in the normal form. Nov 12 '15 at 17:17