ListTools - Maple Programming Help

Home : Support : Online Help : Programming : Data Types : Tables, lists, and sets : ListTools Package : ListTools/Slice

ListTools

 Slice
 slices a list into a sequence of sub-lists with the number of elements in each sub-list differing by at most one

 Calling Sequence ListTools[Slice]( L, n ); Slice( L, n );

Parameters

 L - Any list, to be sliced into sub-lists. n - Positive integer, specifying the target number of sub-lists.

Description

 • As the underlying implementation of the ListTools package is a module, it is also possible to use the form ListTools:-Slice to access the command from the package. For more information, see Module Members.
 • The command slices a list into a sequence of sub-lists with the number of elements in each sub-list differing by at most one.
 • If not all the sub-lists are of the same length, the longer lists appear first.
 • Elements in the returned sequence of sub-lists appear in the same order as the original list.
 • If the specified number of sub-lists is greater than the number of elements in the original list, then the list is sliced into sub-lists of single elements, followed by empty sub-lists.

Examples

 > $\mathrm{with}\left(\mathrm{ListTools}\right):$
 > $\mathrm{Slice}\left(\left[\right],1\right)$
 $\left[\right]$ (1)
 > $\mathrm{Slice}\left(\left[\right],2\right)$
 $\left[\right]{,}\left[\right]$ (2)
 > $L≔\left[a,b,c,d,e\right]$
 ${L}{≔}\left[{a}{,}{b}{,}{c}{,}{d}{,}{e}\right]$ (3)
 > $\mathrm{Slice}\left(L,1\right)$
 $\left[{a}{,}{b}{,}{c}{,}{d}{,}{e}\right]$ (4)
 > $\mathrm{Slice}\left(L,2\right)$
 $\left[{a}{,}{b}{,}{c}\right]{,}\left[{d}{,}{e}\right]$ (5)
 > $\mathrm{Slice}\left(L,3\right)$
 $\left[{a}{,}{b}\right]{,}\left[{c}{,}{d}\right]{,}\left[{e}\right]$ (6)
 > $\mathrm{Slice}\left(L,4\right)$
 $\left[{a}{,}{b}\right]{,}\left[{c}\right]{,}\left[{d}\right]{,}\left[{e}\right]$ (7)
 > $\mathrm{Slice}\left(L,5\right)$
 $\left[{a}\right]{,}\left[{b}\right]{,}\left[{c}\right]{,}\left[{d}\right]{,}\left[{e}\right]$ (8)
 > $\mathrm{Slice}\left(L,6\right)$
 $\left[{a}\right]{,}\left[{b}\right]{,}\left[{c}\right]{,}\left[{d}\right]{,}\left[{e}\right]{,}\left[\right]$ (9)

Compatibility

 • The ListTools[Slice] command was introduced in Maple 2020.