5
$\begingroup$

This question already has an answer here:

So if

list={1,2,3}

then output should be

{{},{1},{2},{3},{1,2},{2,3},{1,2,3}}

or in different order.

$\endgroup$

marked as duplicate by Martin Ender, Simon Woods, MarcoB, m_goldberg, user9660 Apr 4 '16 at 4:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10
$\begingroup$

In version 10.4 you can use

Subsequences[{1, 2, 3}]

(* {{}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 2, 3}} *)
$\endgroup$
  • 1
    $\begingroup$ For some reason, the built-in gets quite slower for larger inputs. Timings here. (Also, I find the behaviour to return the empty list once really inconsistent, but apparently that's what the OP wants.) $\endgroup$ – Martin Ender Apr 3 '16 at 18:52
  • $\begingroup$ @MartinBüttner, regarding the linked question RunnyKine's answer here gives four empty lists if you remove the Union bit. $\endgroup$ – Simon Woods Apr 3 '16 at 19:44
  • $\begingroup$ Yes, I'm aware. :) I'll probably update my answer some time this week with additional approaches, Catenate @ instead of Join @@ and new timings. $\endgroup$ – Martin Ender Apr 3 '16 at 20:49
7
$\begingroup$

One way is this:

Catenate@Table[
  Take[list, {i, j}],
  {i, Length[list]}, {j, i, Length[list]}
  ]
$\endgroup$
7
$\begingroup$

One can use SequenceCases starting from version 10.1:

Union@SequenceCases[{1, 2, 3}, {___}, Overlaps -> All]

{{}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 2, 3}}

Or, if you don't want the empty list:

SequenceCases[{1, 2, 3}, {__}, Overlaps -> All]

{{1, 2, 3}, {1, 2}, {1}, {2, 3}, {2}, {3}}

$\endgroup$
6
$\begingroup$
Sort[Join[{{}}, ReplaceList[list, {___, x__, ___} :> {x}]]]

{{}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 2, 3}}

Also:

ClearAll[f]
f[lst_] := With[{indices = List /@ Join @@ (Partition[Range[Length@lst], #, 1] & /@ 
       Range[Length@lst])}, Extract[lst, indices]]

f[{a, b, c, d}]

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

If you need to include {}:

Prepend[f[{a, b, c, d}], {}]
$\endgroup$
  • $\begingroup$ Ah, this is better than mine! About the empty {}, notice that the OP added that to the question only after he posted his own answer with Subsets ... which doesn't return what he asked for even after this update to the question. $\endgroup$ – Szabolcs Apr 3 '16 at 8:46
2
$\begingroup$
in:= Subsets[list]
out:= {{},{1},{2},{3},{1,2},{2,3},{1,2,3}}
$\endgroup$
  • 6
    $\begingroup$ That doesn't return what you asked for. Do you want all subsets, including {1,3} or do you want all sublists of contiguous elements as your example suggests? $\endgroup$ – Szabolcs Apr 3 '16 at 8:35
  • $\begingroup$ @Szabolcs Nice point! $\endgroup$ – Han Xiao Apr 3 '16 at 8:41
  • 3
    $\begingroup$ And do you want the empty set or not? (Your original post suggested that you did not) $\endgroup$ – user1066 Apr 3 '16 at 9:14

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