Any built-in function to generate successive sublists from a list?
lst={a,b,c,d};
ReplaceList[lst,{x__, ___} :> {x}]
Speaking of "common operation":
Table[lst[[;; i]], {i, Length@lst}]
A variant using Take
.
list~Take~# & /@ Range@Length@list
{{a}, {a, b}, {a, b, c}, {a, b, c, d}}
One using NestList
:
NestList[Most, list, Length@list - 1]
{{a, b, c, d}, {a, b, c}, {a, b}, {a}}
Subsets
takes an optional 3rd argument as Subsets[list, {n}, k]
that gives you the k
th sublist of length n
. Since your sublists are in sequence, you'll always need k = 1
. You can then use this as:
MapIndexed[First@Subsets[list, #2, 1] &, list]
(* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *)
Another alternative would be:
Reverse@Most@NestWhileList[Most, list, # != {} &]