WLJS LogoWLJS Notebook

SplitBy

SplitBy[list, f] splits list into sublists consisting of runs of successive elements that give the same value when f is applied.

SplitBy[list, {f1, f2, ...}] recursively splits list into sublists by testing elements successively with each of the fi.

Examples

Split by a function:

SplitBy[{1, 2, 2, 3, 1, 1, 1}, EvenQ]
(* {{1}, {2, 2}, {3}, {1, 1, 1}} *)

Split strings by first character:

SplitBy[{"apple", "ant", "banana", "cherry"}, First@Characters[#] &]
(* {{"apple", "ant"}, {"banana"}, {"cherry"}} *)

Please visit the official Wolfram Language Reference for more details.

On this page