As a minimal example, suppose I have a list of the integers from 1 to 25. Suppose I want to use Partition
to partition the list into sublists of length 10 but without dropping the "end" elements.
For example, this code
list = Range[1, 25];
Partition[list, 10]
gives: {{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,18,19,20}}
, where the integers 21 through 25 have been dropped. What if I want to keep those integers? I would like Partition
to partition the list into sublists of length 10 only when possible (i.e., keeping the "end" elements). How can I do this?
In the Partition
documentation, this entry looks like a possibility:
Partition[list, n, d, {kL, kR}, {}]
uses no padding, and so can yield sublists of different lengths.
So I tried:
Partition[list, 10, 0, {1, 1}, {}]
but this did not work (according to the documentation {kL, kR} = {1, 1}
means "allow maximal overhang at the end").
On the other hand, this seems to work:
Partition[list, 10, 10, 1, {}]
which gives the correct output: {{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,18,19,20},{21,22,23,24,25}}
.
Is this the best way to solve the problem, or am I making a conceptual mistake? I obtained the idea Partition[list, 10, 10, 1, {}]
from the following documentation, but conceptually I am not really sure what it is doing:
Use no padding, so later sublists can be shorter:
Partition[{a,b,c,d,e,f,g},3,3,1,{}]
{{a,b,c},{d,e,f},{g}}