1
$\begingroup$

This question already has an answer here:

I have a 1D list list={1,2,3,4,5,6,7,8,9} and I would like to partition it according to some specified sizes sizes={2,1,2,4}, where each element in sizes is the number of elements to include in each sublist. The desired result will be result={{1,2},{3},{4,5},{6,7,8,9}}. What would be a simple way to do this with list manipulations?

$\endgroup$

marked as duplicate by corey979, b3m2a1, Kuba list-manipulation Apr 4 at 21:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ Look here, at Internal`PartitionRagged. $\endgroup$ – corey979 Apr 4 at 20:48
  • 5
    $\begingroup$ @corey979 this is also now in the "System`" context as TakeList $\endgroup$ – b3m2a1 Apr 4 at 20:51
  • 2
    $\begingroup$ But Internal`PartitionRagged is usually faster than TakeList... $\endgroup$ – Henrik Schumacher Apr 4 at 20:56
  • $\begingroup$ @b3m2a1 Thank you. This is really a neat function. According to the documentation, it is new in 11.2. $\endgroup$ – nanjun Apr 4 at 20:58

Browse other questions tagged or ask your own question.