# Tuples of elements from list excluding anything with repeated values

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.

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

lst = {a, b, s, t}

tlst = Tuples[lst, 3]

srtlst = Cases[tlst, Except[ {___, x_, ___, x_, ___}]]


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

list = Tuples[{a, b, s, t}, 3];


Using Select and CountDistinct:

Select[list, CountDistinct[#] == 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}}

list = Tuples[{a, b, s, t}, 3];


Using SequenceSplit (new in 11.3) with OrderlessPatternSequence

Join @@ SequenceSplit[list, {{OrderlessPatternSequence[a_, a_, _]}}]


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

Using DeleteCases with OrderlessPatternSequence

DeleteCases[{OrderlessPatternSequence[a_, a_, _]}] @ list


(* same result *)

Timings with Tuples[Range[20], 3] (8000 elements)

Permutations (Carl Woll): 0.00015 sec

Cases (Anxon Pués): 0.011 sec

DeleteCases (Eldo): 0.016 sec

SequenceSplit (Eldo): 1.019 sec

• It's nice to see these newer functions get advertised on MSE. But bear in mind that some usages might be unreasonably slow. And for this problem, the method shown by @CarlWoll is The Way To Do It. Commented Jan 27 at 22:40
• Thank you, Daniel, I included the timings. Especially SequenceSplit gets horribly slow with longer lists.
– eldo
Commented Jan 28 at 8:12