# Split a list at certain values without omission

This sounds simple and very likely a duplicate but I couldn't find an answer. Is there a quick functional way to split a list at certain values (multiple locations) while keeping those values in the subsists?

As an example:

seq1 = Range[10];
splitat1 = {1};
SequenceSplit[seq1, splitat1]


results in:

{{2, 3, 4, 5, 6, 7, 8, 9, 10}}

Here is my first problem, 1 is missing. Maybe I should be using a different method than SequenceSplit? Furthermore, what if I want to split at several locations? I tried this:

splitat2 = {{1}, {6}};
FlattenAt[SequenceSplit[seq1, #] & /@ splitat2, 1]


but obviously I am doing it wrong as I am splitting the list several times and then they repeat:

{{2, 3, 4, 5, 6, 7, 8, 9, 10}, {{1, 2, 3, 4, 5}, {7, 8, 9, 10}}}

How would I get this result?

{{1},{2, 3, 4, 5, 6}, {7, 8, 9, 10}}

Now having it split at the first element is a little bit harder probably, so even if there is chance splitting at {2,6} results in either

{{1,2},{3, 4, 5, 6}, {7, 8, 9, 10}}

or

{{1},{2, 3, 4, 5}, {6,7, 8, 9, 10}}

that would still solve my problem yet in a different way.

• @HighPerformanceMark thanks, I couldn't get that to work yet. The criteria basically the split points are given as a list of members. Mar 3, 2022 at 17:26

You can use Split:

Split[seq1, # != splitat1[[1]] &]

{{1}, {2, 3, 4, 5, 6, 7, 8, 9, 10}}

Split[seq1, # != 5 &]

{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}

Split[seq1, ! MatchQ[2 | 6] @ # &]

{{1, 2}, {3, 4, 5, 6}, {7, 8, 9, 10}}

Split[seq1, Nor[# == 2, # == 6] &]

{{1, 2}, {3, 4, 5, 6}, {7, 8, 9, 10}}

• These are very promising, thanks! Is there a way I can construct the pattern to pass the split points as another list? let's say splitat= {2,5,7,9}? maybe MemberQ? Mar 3, 2022 at 17:22
• !MemberQ[{2,5,7,9},#]& or !MatchQ[Alternatives@@{2,5,7,9}]@#& should work.
– kglr
Mar 3, 2022 at 17:28
• This a great, thanks a lot! Split[seq1, ! MemberQ[splitat, #] &] works beautifully. Mar 3, 2022 at 17:31

Let's say you have a list of numbers from 1-20 and you want to split it in sections given as follows:

alist = Range[20]
sections = {1, 5, 4, 6}


Use FoldPairList

FoldPairList[TakeDrop, alist, sections]


{{1}, {2, 3, 4, 5, 6}, {7, 8, 9, 10}, {11, 12, 13, 14, 15, 16}}

If you want the remaining part of the list as well:

FoldPairList[TakeDrop, alist, {Splice[sections],
Length@alist - Total@sections}]

{{1}, {2, 3, 4, 5, 6}, {7, 8, 9, 10}, {11, 12, 13, 14, 15, 16}, {17,
18, 19, 20}}

• Thanks, this solution is nice too. I just converted my split element list to the sections as you have defined by sections = Prepend[Differences[splitat], First@splitat] and got the same results as @kglr solution. Mar 3, 2022 at 17:58