# SequenceReplace is incorrectly modifying the list when a pattern partially matches

(Below is a big simplification of my actual use-case which reproduces the problem with SequenceReplace. Please assume my use of SequenceReplace is necessary).

I have a list of symbols with subscripts and square brackets. E.g.

{ Subscript[a,1][0], Subscript[b,0][2], Subscript[b,1][1], Subscript[c,2][0]] }


or better visually:

I wish to modify the list such that neighbouring b symbols with the same subscript value are removed. I am attempting this using the pattern

SequenceReplace[mylist, {Subscript[b, i_][_], Subscript[b, i_][_]} :> Nothing]


though this incurs a strange error. When the subscripts do match, it removes them fine:

but when they don't match, it undesirably puts them in their own sub-list!

Here, the expected output was the unchanged mylist.

This seems like a strange and undocumented behaviour; is this a bug, or am I not understanding something?

I notice this only occurs when both subscripts and [args] in present in my pattern. For example, without the args (e.g. Subscript[b,_], Subscript[b,_] :> Nothing) this sublist behaviour does not occur.

Furthermore, this behaviour seems specific to SequenceReplacement and not my pattern, since the unnecessarily verbose use of ReplaceAll doesn't exhibit it!

• It does seem like either a bug or something that should be explained in the documentation. It's probably worth reporting to Wolfram and seeing what they have to say. Jun 26 '19 at 18:10
• This was indeed a bug, and I've been told it's patched in the current12.0 release Jun 12 '20 at 3:01

## 1 Answer

Not sure why that happens, but here's a workaround:

list1 = {Subscript[a,1][0],Subscript[b,0][2],Subscript[b,1][1],Subscript[c,2][0]};
list2 = {Subscript[a,1][0],Subscript[b,0][2],Subscript[b,0][1],Subscript[c,2][0]};

SequenceReplace[list1, {PatternSequence[Subscript[b,i_][_], Subscript[b,i_][_]]} -> Nothing]
SequenceReplace[list2, {PatternSequence[Subscript[b,i_][_], Subscript[b,i_][_]]} -> Nothing]


{Subscript[a, 1][0], Subscript[b, 0][2], Subscript[b, 1][1], Subscript[c, 2][0]}

{Subscript[a, 1][0], Subscript[c, 2][0]}

• Thanks very much! Miraculously, though my 'MWE' still shows this bug, my original use-case has suddenly started working (I think due to some other unrelated rules in SequenceReplace). I think I need to lie down Jun 26 '19 at 19:11