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
    
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
add comment

3 Answers

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.