# Replacing a specific combination in a list

I have the following list:

l={{a},{a,b},{a,d},{b,d},{a,b,c},{a,d,b}}


I want to replace a,b with x such that I get:

{{a},{x},{a,d},{b,d},{x,c},{x,d}}


So where every there is a,b in elements of the list I need to to be replaced by letter x, I tried Replace and ReplacePart but I can not figure it out.

• Since the last element transforms like {a,d,b} -> {x,d}, does that mean that the order doesn't matter at all in the sublists? For instance, should {a,a,a,d,b,b,b} become {x,x,x,d}? – Marius Ladegård Meyer Jul 20 '18 at 15:43
• Order does not matter and yes it can be like {x,x,x,d}. My point is that let us suppose we do not want to use Sort. How the list can be scanned and where ever there is a,b we change it to x. – Wiliam Jul 20 '18 at 15:45
• By "order does not matter," I presume {b, a} would also become {x}? – JungHwan Min Jul 20 '18 at 15:47
• That's right :) – Wiliam Jul 20 '18 at 15:52
• What about {a, a, b, d}? do you want {a, d, x} or {d, x} ? – rhermans Jul 20 '18 at 16:03

OrderlessPatternSequence would work:

l //. {OrderlessPatternSequence[a, b, p___]} :> {x, p}


If it is guaranteed that a and b occur at most once per element, then ReplaceAll is sufficient (instead of ReplaceRepeated):

l /. {OrderlessPatternSequence[a, b, p___]} :> {x, p}

• it seems you don't need ReplaceRepeated (ReplaceAll will do). (+1) – kglr Jul 20 '18 at 16:01
• @kglr without ReplaceRepeated, {a, a, b, b} would become {x, a, b}, which I inferred isn't desirable (from the second comment under OP). – JungHwan Min Jul 20 '18 at 16:07
• ah, i see. Thanks. – kglr Jul 20 '18 at 16:10
• @kglr edited answer to reflect that. – JungHwan Min Jul 20 '18 at 16:17
• If performance is important consider using Replace at levelspec {1}. – Mr.Wizard Jul 20 '18 at 19:13

Something like

l //. {pre___, a, mid___, b, post___} | {pre___, b, mid___, a, post___}  :> {pre, x, mid, post}


should work.

For the case with no repeated elements you can also use SequenceReplace in combination with OrderlessPatternSequence:

SequenceReplace[{OrderlessPatternSequence[a, p___, b]} -> Sequence[x, p]] /@ l


{{a}, {x}, {a, d}, {b, d}, {x, c}, {x, d}}

Given that it's not clear what is wanted for repeated instances, i.e. if for {a, a, b, d} you want {a, d, x} or {d, x}, I offer another kind solution, that works in the second. If there are no repetitions the result is the same, but a bit slower.

repl = If[
SubsetQ[#, {a, b}]
, Prepend[Complement[#, {a, b}], x]
, #] &;

repl /@ l
(* {{a}, {x}, {a, d}, {b, d}, {x, c}, {x, d}} *)