Find all ways to split a list into k sublists (of different length)

I would like to write an efficient code for splitting a set into k disjoint subsets, whose union would be the input set. The input set is represented by sorted lists with no repetitions, and the resulting sets are also expected in this form. For example ListCompositions[{a,b,c,d},2]

produces

{{},{a,b,c,d}}, {{a},{b,c,d}}, {{b},{a,c,d}}, {{c},{a,b,d}}, {{d},{a,b,c}}, {{a,b},{c,d}}, {{a,c},{b,d}}, {{a,d},{b,c}}

I know that Mathematica is not an optimal tool for this type of problems, but the code won't be used for gigantic sets.

EDIT: I'm now aware of KSetPartitions function, but it does not solve the problem completely since it produces only partitions with no empty sets. Thus, the partitions like {{},{a,b,c,d}} from the original example are not obtained.

• seen ResourceFunction["KSetPartitions"]: link?
– kglr
Feb 2, 2023 at 13:08
• in addition to @kglr there's also SetPartitions[myList] with myList={a,b,c,d}
– bmf
Feb 2, 2023 at 13:22
• another would be Internal PartitionRagged[myList, #] & /@ Apply[Join, Permutations /@ IntegerPartitions[Length[myList]]]
– bmf
Feb 2, 2023 at 13:27
• @wedelfach you say ResourceFunction["KSetPartitions"][yourlist, 2] gives all you need, except that is missing the one solution { {}, yourlist }? Woudn't be trivial to just add that one missing case? Feb 17, 2023 at 11:51
• Right, it is indeed trivial in this case, but when it gets to Length[list]>k>2 a bit more effort is necessary. I have solved it using auxiliary elements which are added to the list, and the removed, from the partitions. Feb 17, 2023 at 12:28

yourList = (Indexed[x, #1] & ) /@ Range[5]