# Taking a portion of a list

Given a list of numbers, e.g. aa={1,2,3,4,5,6,7,8,9,10}; bb={6,7}, how do I take the portion of the list that matches a particular pattern, for example, "6,7". I tried

ix=Position[aa,#]/@bb
newaa=aa[[ix;;Length[aa]]]


and was not not a valid span specification. Your help would be much appreciated.

• try SequencePosition[aa, bb] and/or SequenceCases[aa, bb]?
– kglr
Commented May 2, 2020 at 20:38
• The real issue is not whether I can determine the position of this pattern, indeed ix=Position[aa,bb] does, but it is given in the form of {{ }} which cannot be used in the subsequent statement, i.e., newaa=aa[[ix;;Length[aa]]] because aa is given as {{13}}, not suitable as a "valid span specification". Commented May 3, 2020 at 4:41
• what is the desired result (newaa)? Is it {6, 7, 8, 9, 10}?
– kglr
Commented May 3, 2020 at 4:46
• The portion of the list after 6,7 should be {8,9,10} or if I get {6,7,8,9,10} that would be acceptable also. Commented May 3, 2020 at 5:14
• Can you expand on what you mean by “...take the portion of the list...”? Do you wish to remove it from the list and have what remains? Or do you wish to have the portion of the list that follows the desired sequence pattern? Please, can you clarify this? Commented May 3, 2020 at 11:28

aa = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bb = {6, 7};


1. You can use SequenceCases as follows:

First @ SequenceCases[aa, Append[bb, ___]]

 {6, 7, 8, 9, 10}


Alternatively,

First @ SequenceCases[aa, Append[bb, c___] :> {c}]

 {8, 9, 10}


2. If you have to use Position, you can do

ix = 1 + Last @ Flatten[Position[aa, #] & /@ bb]
newaa = aa[[ix ;;]]

 {8, 9, 10}


3. Using SequencePosition instead of Position:

ix2 = 1 + Last @ Flatten @ SequencePosition[aa, bb];
newaa2 = aa[[ix2 ;;]]

 {8, 9, 10}

SequenceSplit[aa,bb][[2]]


{8, 9, 10}

• I did not even know that existed... Commented May 5, 2020 at 10:00
• This is, imo, the canonical answer. “Oh, right, of course there’s a function to do exactly that!” Commented May 5, 2020 at 12:40

Another couple of alternative answers, or more variations on a theme, if you will:

Last@@SequencePosition[aa,bb]//aa[[#+1;;]]&

aa[[1+Last@##;;]]&@@SequencePosition[aa,bb]

#[[1+Last@@SequencePosition[#,bb];;]]&@aa



All give the desired result.

aa = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bb = {6, 7};

Replace[aa, {h___, PatternSequence @@ bb, g___} :> {g}]


{8, 9, 10}

or if you want the pattern as well:

Replace[aa, {h___, k : PatternSequence @@ bb, g___} :> {k, g}]


{6, 7, 8, 9, 10}

Furthermore, Replace allows a levelspec argument to provide more flexibility.

aa = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
bb = {6, 7};

Drop[aa, {1, SequencePosition[aa, bb][[-1, -1]]}


{8, 9, 10}

Some of the answers give wrong results with a more complicated list.

aa = {5, 7, 4, 3, 1, 1, 8, 4, 3, 9, 1, 2, 3, 4};
bb = {4, 3};


The sequence 4, 3 occurs twice.

1. Take after last 4, 3

Drop[aa, {1, SequencePosition[aa, bb][[-1, -1]]}]


{9, 1, 2, 3, 4}

2. Take after first 4, 3

Drop[aa, {1, SequencePosition[aa, bb][[+1, -1]]}]


{1, 1, 8, 4, 3, 9, 1, 2, 3, 4}