# How to create a list of pairs from 1d list(s)?

Say I've got two 1-dimensional lists

A = {1,2,3}
B = {a,b,c}


How do I

1. create a list of all pairs from list A such that the result looks like {{1,2},{1,3},{2,3}}?
2. create a list of all pairs from lists A and B such that the result looks like {{1,a},{1,b},{1,c},{2,a},{2,b},{2,c},{3,a},{3,b},{3,c}}?

I would also need both options, one with no duplicates as above and one with duplicates, e.g. one where the result from case 1 also includes {{2,1},{3,1},{3,2}}.

• Thanks for the great replies! I could have chosen any of them as the accepted answer.. – janitor048 Apr 19 '12 at 19:43

ordering not valid:

Subsets[A, {2}]

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


or with ordering

Permutations[A, {2}]


Outer[List, A, B]

{{{1, a}, {1, b}, {1, c}}, {{2, a}, {2, b}, {2, c}}, {{3, a}, {3, b}, {3, c}}}


or exactly

Outer[List, A, B] // Flatten[#, 1] &

 {{1, a}, {1, b}, {1, c}, {2, a}, {2, b}, {2, c}, {3, a}, {3, b}, {3, c}}


As to question 1

Subsets[A, {2}]


Gives

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


As to question 2

Tuples[{A, B}]


Gives

{{1, a}, {1, b}, {1, c}, {2, a}, {2, b}, {2, c}, {3, a}, {3, b}, {3,
c}}


Question 1 with dupes:

Tuples[A, {2}]


Gives

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


You can see this also includes {1, 1}. If all you want is the same result as before but reversed, you can do Join[#, Reverse/@#]&@Subsets[A, {2}] or use Permutations[A, {2}] @EliLansey suggested in his answer

Question 2 with duples, I'm not sure what you mean. Perhaps Tuples[{A, B}]~Join~Tuples[{B, A}] (or, equivalently, Join[#, Reverse/@#]&@Tuples[{A, B}]) ?

Here's an answer for a variant of question #2 for those who were looking for something slightly different, as I was.

The original question #2 is asking for a generalized outer product (matching up every element of the first list with every element of the second list), which is why Artes correctly gives Outer[List, A, B] as the solution. Here List is the operator to perform on each pair of elements.

This lead me to solve my own problem, which was to create a generalized inner product list (matching up the i-th element of the first list with the i-th element of the second list), which looks like {{1,a},{2,b},{3,c}}.

Inner[List, A, B, List]


gives

{{1, a}, {2, b}, {3, c}}


Here, List replaces both the multiplication and addition functions in the usual inner product.

This is useful if you have two lists for separate 1D histograms that you want to combine into a 2D histogram with Histogram3D.

## Regarding question 1:

Permutations[A, {2}]


gives

(* {{1, 2}, {1, 3}, {2, 1}, {2, 3}, {3, 1}, {3, 2}} *)


For all possible combos, use:

Tuples[A, 2]


which gives

(* {{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}} *)


## And, for question 2:

Outer[{#1, #2} &, A, B]


gives

(* {{{1, a}, {1, b}, {1, c}}, {{2, a}, {2, b}, {2, c}}, {{3, a}, {3, b}, {3, c}}} *)


If you want it unsorted:

Flatten[Outer[{#1, #2} &, A, B], 1]


Regarding question 2:

Distribute[{{1, 2, 3}, {a, b, c}}, List]


gives

(* {{1, a}, {1, b}, {1, c}, {2, a}, {2, b}, {2, c}, {3, a}, {3, b}, {3,
c}} *)