Skip to main content
3 of 5
remove uneeded extra line
Nasser
  • 156.1k
  • 12
  • 173
  • 396

Any built-in function to generate successive sublists from a list?

Given

lst = {a, b, c, d} 

I'd like to generate

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

but using built-in functions only, such as Subsets, Partitions, Tuples, Permutations or any other such command you can choose. But it has to be done only using built-in commands. You can use a pure function, if inside a built-in command, i.e. part of the command arguments. That is OK.

It is of course trivial to do this by direct coding. One way can be

lst[[1 ;; #]] & /@ Range[Length[lst]] (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

or even

LowerTriangularize[Table[lst, {i, Length[lst]}]] /. 0 -> Sequence @@ {} (* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *) 

But I have the feeling there is a command to do this more directly as it looks like a common operation, but my limited search could not find one so far.

Sorry in advance if this was asked before. Searching is hard for such general questions.

Nasser
  • 156.1k
  • 12
  • 173
  • 396