Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Partition a list into roughly equal lengths
ResourceFunction["NearEqualPartition"][list,n] partitions list into n nonoverlapping sublists of near-equal length. |
When the length of list is divisible by the number of partitions n, all the returned lists will be equal in length. In other cases, ResourceFunction["NearEqualPartition"] maximizes the length of the smallest partition.
Partition a list into three non-overlapping sublists:
In[1]:= |
Out[1]= |
When the list cannot be partitioned in equal lengths, the first sublists are the longest:
In[2]:= |
Out[2]= |
When the length of the list is divisible by the partition count, all lists are the same length:
In[3]:= |
Out[3]= |
No elements of the input are discarded and no extra elements introduced, so for a one-dimensional list the inverse operation is Flatten:
In[4]:= |
Out[4]= |
The lengths of the sublists should not differ by more than 1:
In[5]:= |
Out[5]= |
This work is licensed under a Creative Commons Attribution 4.0 International License