1
$\begingroup$

Say I have a list $L$ where the elements can be sorted into some canonical order. I want to use Tuples[L,m] but I only want the output lists to be sorted and without repetition at any level. What I've currently got is

pretup = Tuples[L, m];
nextup = Table[Sort[pretup[[i]]], {i, 1, Length[L]^m}];
nextup = DeleteDuplicates[nextup];
finaltup = {};
Do[If[Length[DeleteDuplicates[nextup[[i]]]] == Length[nextup[[i]]], 
  AppendTo[finaltup, nextup[[i]]]], {i, 1, Length[nextup]}]
finaltup

but this is very inefficient. It first constructs all the possible combinations, then sorts them, then filters out duplicate sublists, then filters out duplicates within the sublists. Is there a faster way to do this? If so, what is it?

$\endgroup$
3
  • 2
    $\begingroup$ Please assign values to L and m. Thanks. $\endgroup$
    – Syed
    Commented Apr 21, 2022 at 13:49
  • $\begingroup$ The function you are looking for is: Subsets. E.g.: Subsets[L, {m}] $\endgroup$ Commented Apr 21, 2022 at 16:37
  • $\begingroup$ @DanielHuber can you please make this an answer? $\endgroup$
    – lericr
    Commented Apr 21, 2022 at 16:54

1 Answer 1

5
$\begingroup$

The function you are looking for is: Subsets.

E.g. for a list with n=5 elements and tuples with m=3 elements (note, you should not use capitalized variable names as those are used by the system):

n = 5;
m = 3;
l = Range[n];
Subsets[l, {m}]

enter image description here

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.