# Native function that gives sequential sub-sequences of a list? [duplicate]

I am very surprised not to find this function in the help (or in other questions on this site, which come close)

Is there a Mathematica function f such that

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


or must I construct it?

• Thank you all so much for your quick answers! Going with myfun[x_] := Take[x, #] & /@ Range[Length[x]] Commented Jan 15, 2018 at 2:20
• Recommend myfun[x_?VectorQ] := ... Commented Jan 15, 2018 at 2:35

Take[{a, b, c, d}, #] & /@ Range[4]

(* {{a}, {a, b}, {a, b, c}, {a, b, c, d}} *)


Another possibility:

Reverse @ NestList[Most, {a,b,c,d}, 3]


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

• In the same 'vein': FoldList[Flatten[{##}] &, Nothing, {a, b, c, d}] Commented Jan 15, 2018 at 10:28

Two ideas

f1[l_List] := Rest[FoldList[Append, {}, l]];
f2[l_List] := Table[Take[l, i], {i, Length[l]}]

ReplaceList[{a, b, c, d}, {x__, ___} :> {x}]


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

Table[{a, b, c, d}[[;; i]], {i, 4}]


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

{a, b, c, d}[[;; #]] & /@ Range@4


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

Partition[{a, b, c, d}, 4, 1, -1, {}]


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

Extract[{a, b, c, d}, List /@ Range @ Range @ 4]


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

For something different

f[x_List] :=
LowerTriangularize@ConstantArray[x, Length[x]] /. 0 -> Nothing

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


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