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?

  • 3
    $\begingroup$ Look here, at Internal`PartitionRagged. $\endgroup$ – corey979 Apr 4 '19 at 20:48
  • 5
    $\begingroup$ @corey979 this is also now in the "System`" context as TakeList $\endgroup$ – b3m2a1 Apr 4 '19 at 20:51
  • 2
    $\begingroup$ But Internal`PartitionRagged is usually faster than TakeList... $\endgroup$ – Henrik Schumacher Apr 4 '19 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 '19 at 20:58

Browse other questions tagged or ask your own question.