Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Split a list at specific indices
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]. |
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]= |
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]= |
This work is licensed under a Creative Commons Attribution 4.0 International License