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.

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

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

5 Answers 5

up vote 6 down vote accepted

Ad. 1

ordering not valid:

Subsets[A, {2}]
{{1, 2}, {1, 3}, {2, 3}}

or with ordering

Permutations[A, {2}]

Ad. 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}}
share|improve this answer

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.

share|improve this answer

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}} *)
share|improve this answer

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}]) ?

share|improve this answer

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]
share|improve this answer

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.