0
$\begingroup$

What I would like to do is the following.

For a given list of elements; say (0,1,2,3,4) I would like to obtain all possible combinations of five, but not the ones with ANY repeated values. That is I don't want to see anything with two elements equal like (0,0,1,2,3), (0,1,2,3,0), etc etc.

Tuples gives all possible combinations back

list = {0, 1, 2, 3, 4};
Tuples[list, 5]

I found this answer Tuples Without Replacements

But this is not I want to do.

I also tried to use Subsets and Reverse

Subsets[list, {5}]
Reverse /@ Subsets[list, {5}]

But the result of the above is to give only the $(0,1,2,3,4)$ and $(4,3,2,1,0)$.

I checked a couple of other replies here and I have not been able to find anything relevant to get me started.

Thanks in advance.

$\endgroup$
4
  • 2
    $\begingroup$ try Select[Tuples[list, 5],Length@Union@#==5&] $\endgroup$
    – ZaMoC
    Oct 13, 2018 at 9:27
  • 5
    $\begingroup$ or Permutations@list $\endgroup$
    – ZaMoC
    Oct 13, 2018 at 9:29
  • $\begingroup$ Great reply. Thanks a lot. $\endgroup$
    – user49048
    Oct 13, 2018 at 9:36
  • $\begingroup$ I believe this question is a dupe. $\endgroup$ Oct 13, 2018 at 10:32

2 Answers 2

1
$\begingroup$
lst = {a, b, s, t}

tlst = Tuples[lst, 3]

srtlst = Cases[tlst, Except[ {___, x_, ___, x_, ___}]]
$\endgroup$
3
$\begingroup$

Just use Permutations:

Permutations[lst]

{{a, b, s, t}, {a, b, t, s}, {a, s, b, t}, {a, s, t, b}, {a, t, b, s}, {a, t, s, b}, {b, a, s, t}, {b, a, t, s}, {b, s, a, t}, {b, s, t, a}, {b, t, a, s}, {b, t, s, a}, {s, a, b, t}, {s, a, t, b}, {s, b, a, t}, {s, b, t, a}, {s, t, a, b}, {s, t, b, a}, {t, a, b, s}, {t, a, s, b}, {t, b, a, s}, {t, b, s, a}, {t, s, a, b}, {t, s, b, a}}

If you want to restrict to 3-element sublists as in @Anxon's answer use the second argument of Permutations:

Permutations[lst], {3}]

{{a, b, s}, {a, b, t}, {a, s, b}, {a, s, t}, {a, t, b}, {a, t, s}, {b, a, s}, {b, a, t}, {b, s, a}, {b, s, t}, {b, t, a}, {b, t, s}, {s, a, b}, {s, a, t}, {s, b, a}, {s, b, t}, {s, t, a}, {s, t, b}, {t, a, b}, {t, a, s}, {t, b, a}, {t, b, s}, {t, s, a}, {t, s, b}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.