# Pattern matching - comparing two lists

I would like to compare two different lists to see if the shorter one is a member of the larger e.g. is the list {2,3}, with that sequence of elements, a member of {1,2,3,4,5}? I have tried using MemberQ[] and Cases[] but they do not seem work when comparing two lists. Thanks for pointing me in the right direction.

• SubsetQ[{1,2,3,4,5},{2,3}] yields True. Unless I'm mistaken, you're looking for a subset test, so this should be exactly what you are looking for. Oct 7, 2014 at 20:14
• MatchQ[{1, 2, 3, 4, 5}, {___, 2, 3, ___}] (with 3 _'s each so it will match even if the longer begins with or ends with the shorter) yields True. That is if you are trying to find if your shorter one appears in that order somewhere within the longer one.
– Bill
Oct 7, 2014 at 20:30

I assume you want to find an exact match, instead of just that individual members of the subset are found in the larger list. This can be quite easily be accomplished with the following:

ClearAll[subsetInOrderQ]
subsetInOrderQ[set_List, subset_List] := MatchQ[set, {___, Sequence @@ subset, ___}]

subsetInOrderQ[{1, 2, 3, 4, 5}, {2, 3}]

(* True *)

subsetInOrderQ[{1, 2, 3, 4, 5}, {2, 4}]

(* False *)


Above can also be written in a form without MatchQ:

ClearAll[subsetInOrderQ]
subsetInOrderQ[{___, subset___, ___}, {subset___}] := True
subsetInOrderQ[_, _] := False


Let me stay very close to your question

short = {2, 3};

long = Range @ 5;

par = Partition[long, Length @ short, 1];

MemberQ[par, short]


True

Expanding a little bit

short = Range[2, 4];
long = Range ;

par = Partition[long, Length @ short, 1];

MemberQ[par, short]


True

IntervalMemberQ[Interval[{1, 5}], Interval[{2, 4}]]


True

Update

short = Range @ 4;
long =  Range @ 5;

IntervalMemberQ[Interval[long[[{1, -1}]]], Interval[short[[{1, -1}]]]]


True

NumberLinePlot[{
Interval[ long[[{1, -1}]]],
Interval[short[[{1, -1}]]]},
PlotTheme -> "Detailed"] BTW, an interesting question.

• +1. Happy to bump you to 10K :) Congrats Oct 7, 2014 at 21:09
• Dear @ RunnyKine - A truly gentlemanly bump. I'm beginning to like this club :)
– eldo
Oct 7, 2014 at 21:31
• Hi Eldo - I really appreciate your time on this. Thanks so much. Also this is a great place to learn. Oct 8, 2014 at 8:14
Union[{1, 2, 3, 4, 5}, {2, 3}] === {1, 2, 3, 4, 5}
(*True*)

• Hi Algohi - Thanks for helping. The problem with the above is if you have the same sub-set appearing again: Union[{1, 2, 3, 4, 5, 6, 5, 3, 2, 3}, {2, 3}] === {1, 2, 3, 4, 5, 6} you get 'false' Oct 8, 2014 at 8:12
• If the two lists are sorted then you can use Union[list, {2, 3}] === Union[list] Oct 9, 2014 at 16:54

Version 10

ClearAll[foo]
foo = Or@@(SubsetQ @@ # &/@{{##},{#2, #}})&

{foo[{1,2,3,4},{3,4}],foo[{2,1},{1,2,3,4}]}
(* True, True *)


Version 9

ClearAll[sQ, foo2]
sQ = And @@ Function[{x}, MatchQ[x, Alternatives @@ #]] /@ #2 &;
foo2 = Or @@ (sQ @@ # & /@ {{##}, {#2, #}}) &

{foo2[{1, 2, 3, 4}, {2, 3}], foo2[{3, 1}, {1, 2, 3, 4}]}
(* {True, True} *)


or

ClearAll[foo3];
foo3 = With[{x = ##}, Or @@ (Intersection@x == Union@# & /@ {x})] &

{foo3[{1, 2, 3, 4}, {2, 3}], foo3[{3, 1}, {1, 2, 3, 4}]}
(* {True, True} *)


or (* generalizing @Algohi's answer to allow arguments in any order *):

ClearAll[foo4];
foo4 = With[{x = ##}, Or @@ (Union@x == Union@# & /@ {x})] &

{foo4[{1, 2, 3, 4}, {2, 3}], foo4[{3, 1}, {1, 2, 3, 4}]}
(*{True,True}*)

• Thanks Kguler, everyone has been so helpful. I'll go through the code you provided. Appreciate your time. Oct 8, 2014 at 8:19
• @awyr_agored, my pleasure. Welcome to mma.se.
– kglr
Oct 8, 2014 at 8:23