Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to build the table

{{1, 1}, {1, 2}, {2, 1}, {2, 2}}

given the list

{1, 2}

I can do it like this

Flatten[Table[{x1, x2}, {x1, {1, 2}}, {x2, {1, 2}}], 1]

But how can I do this given a very long list, say {1, 2, 3, ..., 100}? I've searched the documentation on Nest and Fold, but didn't find a solution.

share|improve this question

marked as duplicate by Michael E2, Öskå, m_goldberg, Rahul Narain, Sjoerd C. de Vries Jul 14 at 17:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Your Table[]command doesn't give the desired result –  belisarius Mar 7 '13 at 2:31
    
@belisarius oops, I've corrected it. Thanks for point it out. –  user0501 Mar 7 '13 at 2:36
    
Related or possible duplicate: mathematica.stackexchange.com/q/15885/121 –  Mr.Wizard Mar 7 '13 at 6:15

4 Answers 4

up vote 8 down vote accepted

I'm not sure what result you want for a larger list but for the example given you can use:

Tuples[{1, 2}, 2]

(*

{{1, 1}, {1, 2}, {2, 1}, {2, 2}}

*)
share|improve this answer
    
thanks for the answer! –  user0501 Mar 7 '13 at 3:17

I think that Outer is what you need for the Cartesian product. In your example:

Outer[{#1, #2} &, {1, 2}, {1, 2}]
(*Out*) {{{1, 1}, {1, 2}}, {{2, 1}, {2, 2}}}

while in the general card-deck example:

Outer[{#1, #2} &, 
    {spades, clubs, hearts, diamonds}, 
    {2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king, ace}]
(*Out*)
{{{spades, 2}, {spades, 3}, {spades, 4}, {spades, 5}, {spades, 6},
    {spades, 7}, {spades, 8}, {spades, 9}, {spades, 10}, {spades, jack}, 
    {spades, queen}, {spades, king}, {spades, ace}}, 
{{clubs, 2}, {clubs, 3}, {clubs, 4}, {clubs, 5}, {clubs, 6}, {clubs, 7},
    {clubs, 8}, {clubs, 9}, {clubs, 10}, {clubs, jack}, {clubs, queen},
    {clubs, king}, {clubs, ace}}, 
{{hearts, 2}, {hearts,3}, {hearts, 4}, {hearts, 5}, {hearts, 6}, {hearts, 7},
    {hearts,8}, {hearts, 9}, {hearts, 10}, {hearts, jack}, {hearts,queen},
    {hearts, king}, {hearts, ace}}, 
{{diamonds, 2}, {diamonds,3}, {diamonds, 4}, {diamonds, 5}, {diamonds, 6},
    {diamonds,7}, {diamonds, 8}, {diamonds, 9}, {diamonds, 10},
    {diamonds,jack}, {diamonds, queen}, {diamonds, king}, {diamonds, ace}}}
share|improve this answer
    
A slightly simpler syntax is: lis=Outer[List, {1, 2}, {1, 2}] To get the parentheses as in the question, you can then flatten it: Flatten[lis, 1] which gives {{1, 1}, {1, 2}, {2, 1}, {2, 2}} –  bill s Mar 7 '13 at 12:16
    
I agree that it's simpler but it is not very illuminating as to what it does IMO, whereas {#1,#2}& "tells" you that it takes elements from the first list and combines them with elements from the second list. –  gpap Mar 7 '13 at 12:26

Here is my answer.

list=Table[i,{i,1,5}]; (* List of your elements*)
cartesianlist=Table[{list[[i]],list[[j]]},{i,1,Length[list]},{j,1,Length[list]}] (* The Cartesian product*)

You may also change it in a way that you have two different lists.

share|improve this answer

This might look like what you are looking for:

Tuples[Table[t, {t, 1, 10, 1}], 2]

(Out)

{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7}, {1, 8}, {1, 
  9}, {1, 10}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}, {2, 6}, {2, 
  7}, {2, 8}, {2, 9}, {2, 10}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 
  5}, {3, 6}, {3, 7}, {3, 8}, {3, 9}, {3, 10}, {4, 1}, {4, 2}, {4, 
  3}, {4, 4}, {4, 5}, {4, 6}, {4, 7}, {4, 8}, {4, 9}, {4, 10}, {5, 
  1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}, {5, 6}, {5, 7}, {5, 8}, {5, 
  9}, {5, 10}, {6, 1}, {6, 2}, {6, 3}, {6, 4}, {6, 5}, {6, 6}, {6, 
  7}, {6, 8}, {6, 9}, {6, 10}, {7, 1}, {7, 2}, {7, 3}, {7, 4}, {7, 
  5}, {7, 6}, {7, 7}, {7, 8}, {7, 9}, {7, 10}, {8, 1}, {8, 2}, {8, 
  3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}, {8, 8}, {8, 9}, {8, 10}, {9, 
  1}, {9, 2}, {9, 3}, {9, 4}, {9, 5}, {9, 6}, {9, 7}, {9, 8}, {9, 
  9}, {9, 10}, {10, 1}, {10, 2}, {10, 3}, {10, 4}, {10, 5}, {10, 
  6}, {10, 7}, {10, 8}, {10, 9}, {10, 10}}
share|improve this answer

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