Partition a list into sublists of unequal lengths according to a list of lengths [duplicate]

This is a conscious duplicate of On generalizing Partition[] (with offsets) to sublists of unequal length: it was seven years ago, and all answers are quite involved; maybe something easier has been added in new versions. Moreover I want a relatively simple subcase of it.

I have a list, say, {x[1],x[2],x[3],x[4],x[5],x[6],x[7],x[8],x[9]}, and another list prescribing lengths of sublists, say, {3,2,4}. I want the result

{x[1],x[2],x[3]}, {x[4],x[5]}, {x[6],x[7],x[8],x[9]}


in form of either a sequence or a list of lists.

My version:

myPartition[l_,s_]:=With[{a = Accumulate[Prepend[s, 0]]},
Table[l[[a[[i]] + 1 ;; a[[i + 1]]]], {i, Length[s]}]


There must exist something more efficient I believe.

• TakeList or the faster but not as flexible and undocumented InternalPartitionRagged. Jan 31 '20 at 21:41

lst = Array[x, 9];
`