Function Repository Resource:

SplitAt

Source Notebook

Split a list before or after elements that satisfy a condition

Contributed by: Sjoerd Smit

ResourceFunction["SplitAt"][list, f,Before]

converts list into a List of lists such that each element for which f returns True starts a new sublist.

ResourceFunction["SplitAt"][list, f,After]

creates a split after each element that satisfies f.

ResourceFunction["SplitAt"][list, f]

is equivalent to ResourceFunction["SplitAt"][list,f,Before].

Examples

Basic Examples (3) 

Split a List at the even numbers, starting a new List each time EvenQ is satisfied:

In[1]:=
ResourceFunction["SplitAt"][
 {5, 7, 3, 4, 5, 5, 3, 7, 8, 9, 1, 8, 7, 9, 10, 2, 10, 4, 1},
 EvenQ,
 Before
 ]
Out[1]=

Split after each even number:

In[2]:=
ResourceFunction["SplitAt"][
 {5, 7, 3, 4, 5, 5, 3, 7, 8, 9, 1, 8, 7, 9, 10, 2, 10, 4, 1},
 EvenQ,
 After
 ]
Out[2]=

Split after each odd number:

In[3]:=
ResourceFunction["SplitAt"][
 {5, 7, 3, 4, 5, 5, 3, 7, 8, 9, 1, 8, 7, 9, 10, 2, 10, 4, 1},
 OddQ,
 After
 ]
Out[3]=

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 04 March 2022

Related Resources

License Information