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.

  • 1
    $\begingroup$ TakeList or the faster but not as flexible and undocumented Internal`PartitionRagged. $\endgroup$ Jan 31, 2020 at 21:41

1 Answer 1


Try TakeList:

lst = Array[x, 9];
TakeList[lst, {3, 2, 4}]

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


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