Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I have a function say $f=x^2$. What I would like is a list (or even better, a list with a grid) which resembles {{1,1},{2,4},{3,9}, ...}. Ideally one should be able to specify the starting point, end point and step interval between values.

This seem like such a simple idea I don't know why I am struggling with it...

share|improve this question
Please have a look at Table. – b.gatessucks Jul 20 '13 at 17:38
Yes, I am aware of table; and have managed to do almost what I desire, but I only obtain one set of values. I.e: {1,4,9,...} as opposed to {{1,1},{2,4},{3,9}} – Ronan Jul 20 '13 at 17:39
@Ronan Table[{x, x^2}, {x, 5}] ? – R. M. Jul 20 '13 at 17:44
... Ok, yes I am that oblivious. Thank you. – Ronan Jul 20 '13 at 17:48
up vote 3 down vote accepted

Using Mathematica's Map function, you can do it this way:

f[x_] := x^2

xList = Range[10]

(* ==> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)

values = Map[f, xList]

(* ==> {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} *)

grid = Transpose[{xList, values}]

==> {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36}, {7, 
  49}, {8, 64}, {9, 81}, {10, 100}}
share|improve this answer
Perfect! I had not even thought of mapping onto the xaxis. This is exactly what I wanted. Many Thanks – Ronan Jul 20 '13 at 17:42

Many functions, such as the one you've defined


are automatically listable. This means that you can get a list of the values directly using


to get the f applied to the first 10 integer values. To get the form you desire, a listing of {x_value, f[x_value]} pairs can be constructed straightforwardly as

Transpose[{Range[10], f[Range[10]]}]

or somewhat more concisely as:

{#, f[#]} & /@ Range[10]
share|improve this answer

With f already defined:

f = #^2 &;

The most direct way is perhaps Array:

Array[{#, f@#} &, 3]

Of course Table may be used as well, as R.M proposed:

Table[{i, f@i}, {i, 3}]

Better for performance is the method using Transpose, but with the given function there is an additional optimization that may be made. The function Power, of which f is composed, is a Listable function and has optimizations for application to vectors and arrays. We could therefore write:

{#, f@#}\[Transpose] & @ Range@3
{{1, 1}, {2, 4}, {3, 9}}

Note that there is no Map here; that is handled by Power itself. See: Alternatives to procedural loops and iterating over lists in Mathematica for some additional examples.

For formatting "a list with a grid" you should look at Grid, TableForm, and MatrixForm. Be aware that these are formatting wrappers(1),(2) that affect output generation but remain part of the expression.

For the sake of formatting alone you may not even need Transpose:

TableForm[{#, f@#} &@Range@3]

TableForm[{{#, f@#} &@Range@3}]  (* note the extra set of {} *)

enter image description here

enter image description here

p.s. Another user posted, then deleted, an answer using MapIndexed, but the form didn't quite make sense. While is it not needed here I suspect that something like this was the intent:

MapIndexed[{First @ #2, #} &, Array[f, 3]]
share|improve this answer
You may want to add another way {x,x^2}/.{x->#}&/@Range@3. – Kuba Jul 21 '13 at 9:24
@Kuba That doesn't use f however. Shorter would be merely {#, #^2}& ~Array~ 3 if we allow such changes. – Mr.Wizard Jul 21 '13 at 9:40
So put there f :) f = #^2 &; {x, f[x]} /. {x -> #} & /@ Range@3 My point was to use ReplaceAll in that way it create an array. I know it is not useful but another method just for fun since it is not closed :) – Kuba Jul 21 '13 at 9:42
How about MapIndexed[{Tr@#2, #} &, Array[f, 3]]? – chyaong Jul 21 '13 at 9:54
@chyanog That's clever! :-) I usually use e.g. #2[[1]] which looks a bit better and is equally terse (but longer to write) with the special characters in a Notebook. In MapIndexed I usually try to preserve extensibility to deeper levels and Tr would change behavior there whereas First would not. – Mr.Wizard Jul 21 '13 at 10:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.