Function Repository Resource:

# SplitAtPositions

Split a list at specific indices

Contributed by: Sjoerd Smit
 ResourceFunction["SplitAtPositions"][lst,{i1,i2,…}] splits lst into a list of sublists, creating a split before each index ik. ResourceFunction["SplitAtPositions"][lst,ind,After] splits after each index instead. ResourceFunction["SplitAtPositions"][lst,ind,Before] is equivalent to ResourceFunction["SplitAtPositions"][lst,ind].

## Details

All indices should be positive and smaller or equal to Length[lst].
Indices will be sorted first and duplicate indices will be ignored.
ResourceFunction["SplitAtPositions"][list,{}] returns {list}.
ResourceFunction["SplitAtPositions"][{},{i1,}] always fails.
The returned list of lists will never contain an empty list. Because of this, ResourceFunction["SplitAtPositions"][{},{}] returns {}.

## Examples

### Basic Examples (3)

Split before indices 2 and 5 so that the sublists after the initial one start at these indices:

 In[1]:=
 Out[1]=

Split after instead of before so that the sublists now end on these indices:

 In[2]:=
 Out[2]=

Explicitly specify to split before each index:

 In[3]:=
 Out[3]=

### Scope (5)

If the list of indices is empty, the original list will be returned with an extra list around it:

 In[4]:=
 Out[4]=

The returned list does not contain empty lists, however:

 In[5]:=
 Out[5]=

Splitting the empty list always fails if the index list is not empty:

 In[6]:=
 Out[6]=

Splitting before 1 has no effect on the initial sublist:

 In[7]:=
 Out[7]=
 In[8]:=
 Out[8]=

Similarly, splitting after the last element does nothing:

 In[9]:=
 Out[9]=
 In[10]:=
 Out[10]=

Splitting at non-existing indices returns \$Failed. Negative indices are not supported:

 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=
 In[13]:=
 Out[13]=

### Properties and Relations (1)

SplitAtPositions[expr,{n},After] is equivalent to TakeDrop[expr,n]:

 In[14]:=
 Out[14]=
 In[15]:=
 Out[15]=

Sjoerd Smit

## Version History

• 2.0.0 – 03 May 2022
• 1.0.0 – 18 April 2022