15
$\begingroup$

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.

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

4 Answers 4

19
$\begingroup$

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}}

*)
$\endgroup$
0
13
$\begingroup$

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}}}
$\endgroup$
3
  • $\begingroup$ 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}} $\endgroup$
    – bill s
    Commented Mar 7, 2013 at 12:16
  • $\begingroup$ 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. $\endgroup$
    – gpap
    Commented Mar 7, 2013 at 12:26
  • $\begingroup$ You can also use it with more than two list: Outer[List,##]&@@{{1,2},{a,b},{x,y}} gives {{{{1, a, x}, {1, a, y}}, {{1, b, x}, {1, b, y}}}, {{{2, a, x}, {2, a, y}}, {{2, b, x}, {2, b, y}}}}. $\endgroup$
    – mlainz
    Commented Jul 30, 2015 at 17:44
1
$\begingroup$

This might look like what you are looking for:

Tuples[Range[10], 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}}
$\endgroup$
0
$\begingroup$

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.

$\endgroup$

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