# Extracting up to $n$ elements after a specified marker from a list

I have a list from which I need to select up to two elements that follow each occurrence of a marker.

For example, the selected marker is {"b","c"} and the list is as follows:

  lis = {"a",{"b","c"},{"d","e"},{"f","g"},"h","i",{"b","c"},{"j","k"},{"l","m"},{"o"}}


This should give:

  res = {{{"d","e"},{"f","g"}},{{"j","k"},{"l","m"}}}


Thanks for any suggestions.

• "Thank you Bill, lericr and Syed for the help! I will time some of these." I am honestly curious and I really do want to understand. What is it that makes people want the most efficient and the fastest possible and need to time this and ... people often make comments like that here, when they are using an interpreted rule based programming language that isn't really intended for speed? Is it because there isn't really anything else so might as well want the fastest possible speed? I'm not being rude, really, I just don't understand and would like to understand. Why is this? Thanks
– Bill
Aug 29 at 5:23

SequenceCases[lis, {{"b", "c"}, a_, b_} :> {a, b}]


That assumes that you don't want a match if fewer than two items follow the {"b","c"}.

If that assumption is invalid, then maybe something like

SequenceCases[lis, {{"b", "c"}, follow__} :> Take[{follow}, UpTo], Overlaps -> True]


Or even

SequenceCases[lis, {{"b", "c"}, follow___} :> Take[{follow}, UpTo], Overlaps -> True]


One way of doing that

Map[Take[lis,{#+1,#+2}]&,Flatten[Position[lis,{"b","c"}]]]


which returns

{{{d,e},{f,g}},{{j,k},{l,m}}}


and if you look at the InputForm of that you see it does include the quotes.

Please test that carefully on all the various kinds of input that you want to give it to make certain that you find any mistakes.

ClearAll[replaceMarkerWithNextKNeighborsInList]

replaceMarkerWithNextKNeighborsInList[list_, marker_, k_ : 2] :=
Map[p |-> list[[p + 1 ;; UpTo[p + k]]]]@PositionIndex[list]@marker


Examples:

lis = {"a",{"b","c"},{"d","e"},{"f","g"},"h","i",{"b","c"},
{"j","k"},{"l","m"},{"o"}};

replaceMarkerWithNextKNeighborsInList[lis, {"b", "c"}]

{{{"d", "e"}, {"f", "g"}}, {{"j", "k"}, {"l", "m"}}}

replaceMarkerWithNextKNeighborsInList[lis, {"b", "c"}, 4]

{{{"d", "e"}, {"f", "g"}, "h", "i"}, {{"j", "k"}, {"l", "m"}, {"o"}}}


With a slightly modified lis that may contain fewer items to pick after each of the markers:

lis = {"a", {"b", "c"}, {"d", "e"}, {"f", "g"}, "h",
"i", {"b", "c"}, {"j", "k"}, {"b", "c"}, {"l", "m"}, {"o"}}


Using Split, Take:

Split[lis, #2 != {"b", "c"} &] //
Select[First@# == {"b", "c"} &] //
Map[Take[#, {2, UpTo}] & ]

{{{"d", "e"}, {"f", "g"}}, {{"j", "k"}}, {{"l", "m"}, {"o"}}}

• Thank you Bill, lericr and Syed for the help! I will time some of these. Aug 29 at 3:53
ClearAll[kRightNeigbors]

kRightNeigbors[l_, k_ : 2] :=
Map[p |-> l[[p + 1 ;; UpTo[p + k]]]] @* PositionIndex[l]


Examples:

kRightNeigbors[lis] @ {"b", "c"}

{{{"d", "e"}, {"f", "g"}}, {{"j", "k"}, {"l", "m"}}}

Quiet @ Check[kRightNeigbors[lis] @ {"b", "d"}, {}]

{}

kRightNeigbors[lis, 4] @ {"b", "c"}

{{{"d", "e"}, {"f", "g"}, "h", "i"}, {{"j", "k"}, {"l", "m"}, {"o"}}}


An alternative method using Extract and Cases:

Extract[#, Transpose@MapThread[Span @@@ {{#1 + 1, #2 - 1}, {#2 + 1, All}} &,
Position[#, {"b", "c"}]]] &@Cases[lis, x_ /; Length[x] == 2]

(*{{{"d", "e"}, {"f", "g"}}, {{"j", "k"}, {"l", "m"}}}*)


Or using SubsetCases:

SubsetCases[lis, {{"b", "c"}, a_List, b_List} :> {a, b}]

(*{{{"d", "e"}, {"f", "g"}}, {{"j", "k"}, {"l", "m"}}}*)


Or using Cases:

Partition[Cases[#, x_ /; Length[x] == 2 && x =!= {"b", "c"}], Count[#, {"b", "c"}]] &@lis

(*{{{"d", "e"}, {"f", "g"}}, {{"j", "k"}, {"l", "m"}}}*)

• The first one with Extract gives off a position error on my setup. Could you please verify?
– Syed
Aug 30 at 5:00
• Hi @Syed! In my configuration it does not generate any position error, and I just verified it. Aug 30 at 5:43
• i.stack.imgur.com/3iqS4.png
– Syed
Aug 30 at 6:04
• i.stack.imgur.com/VjG5f.jpg Aug 30 at 6:10
• I will try to figure it out. Thanks.
– Syed
Aug 30 at 6:15