6
votes

This seems like it should be a piece of cake, but I haven't found the answer in Mathematica's documentation. Say I have two separate lists, for example x={1,2,3,4,5} and y={1,4,9,16,25}. I want to format these lists as a table with each list as a column, like this:

x  y  
1  1  
2  4  
3  9  
4 16  
5 25  

But if I do TableForm[x,y], I get only the first column, like this:

1  
2  
3  
4  
5  

If I do Grid[{x,y}], I get a table, but formatted as rows instead of columns, like this:

1 2 3  4  5  
1 4 9 16 25   

Now, if I have my values as {x,y} pairs, rather than separate lists, then I can get almost what I want,like so:

Input: Table[{n,n^2},{n,1,5}]//TableForm

Output:   
1 1  
2 4  
3 9  
4 16  
5 25  

I say almost, because I'd like to have the variable names at the top of each column, and I'd like the columns justified so that the ones digits are always placed vertically in the "ones place", the tens digits in the "tens place", etc.

So, back to my question: If I have two separate lists of the same length, how can I format them as a table of columns? I checked the MMA documentation for Grid and TableForm, but I couldn't find a way to do it. Did I miss something? If there's no direct way to do it, is there a way to transform two separate lists into pairs of values that could then be formatted in columns using TableForm?

Thanks for any suggestions you might have.

5
Allow me to welcome you to StackOverflow and remind three things we usually do here: 1) As you receive help, try to give it too answering questions in your area of expertise 2) Read the FAQs 3) When you see good Q&A, vote them upusing the gray triangles, as the credibility of the system is based on the reputation that users gain by sharing their knowledge. Also remember to accept the answer that better solves your problem, if any, by pressing the checkmark sign - Dr. belisarius
Nevertheless you should select an answer, a task that many times requires explaining your choice. Read the FAQs. If you don't select one, you are implying that none answer satisfies your needs. - Dr. belisarius
@belisarius Some how I missed your last comment until just now. I've have (belatedely!) selected a "best" answer. Nevertheless, I encourage readers of this question who are learning Mathematica to take a look at some of the other answers, as each will add something to your understanding of how Grid and Tableform work. - eipi10

5 Answers

7
votes

Personally I prefer Grid to TableForm. Maybe something like this?

Some preliminaries:

x = {1, 2, 3, 4, 5};
y = {1, 4, 9, 16, 25};
grid = Transpose@{x, y};
headings = {{Item["x", Frame -> {True, True}], 
    Item["y", Frame -> {True, False}]}};

The following code,

Grid[Join[headings, grid], Alignment -> Right, Dividers -> All, 
 Spacings -> {3, 1}, FrameStyle -> Orange]

gives this as output, for example:

enter image description here

7
votes

Often in Mathematica, you use Transpose to switch the role of row and column.

In[6]:= x = {1,2,3,4,5}; y = {1,4,9,16,25};

In[7]:= {x,y} // Transpose // TableForm

Out[7]//TableForm= 1   1

                   2   4

                   3   9

                   4   16

                   5   25
4
votes

Instead of using Transpose you can use the option TableDirection:

x={1,2,3,4,5};y={1,4,9,16,25};
TableForm[{x,y},TableDirections->Row,TableHeadings->{{"x","y"}}]

a table without legs, but with a head

3
votes
 Grid[Transpose[{x, y}], Alignment -> Right]  

enter image description here

2
votes

I would use:

x = {1, 2, 3, 4, 5};
y = {1, 4, 9, 16, 25};

TableForm[{{x, y}}, TableAlignments -> Right]

enter image description here


Here are some more convoluted examples, demonstrating the way TableForm works. It does get complicated, and I usually have to experiment a bit to get what I want.

a = {1, 2, 3};
b = {4, 5, 6};

{a, b} // TableForm

enter image description here

{{a}, {b}} // TableForm

enter image description here

{{{a}}, {{b}}} // TableForm

enter image description here

{{{a}, {b}}} // TableForm

enter image description here

{{List /@ a, List /@ b}} // TableForm

enter image description here

{{a}, b} // TableForm

enter image description here

{{{a}, b}} // TableForm

enter image description here

{{{a}}, {b}} // TableForm

enter image description here

{{{{a}}, {b}}} // TableForm

enter image description here