How can I make the Cartesian product of a list with itself?
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}}
*)
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}}}