# How can I make the Cartesian product of a list with itself? [duplicate]

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.

-

## marked as duplicate by Michael E2, Öskå, m_goldberg, Rahul, Sjoerd C. de VriesJul 14 '14 at 17:23

Your Table[]command doesn't give the desired result – Dr. 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

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

*)

-
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*)
{{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}}}

-
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
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}}}}. – mlainz Jul 30 '15 at 17:44

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.

-

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

-