Consider a function f[l1_List,l2_List] which is given lists of integers. Whenever two of the lists contain completely disjoint sets of integers OR one of the lists is completely contained in the other, I would like the function to return True. Whenever both lists overlap, but not completely, I would like the function to return False. So for example:








Is there such a function in Mathematica? Or maybe one can implement it conveniently? Thanks for any suggestion!


4 Answers 4


Per comment,

f[l1_, l2_] := MemberQ[{l1, l2, {}}, l1 ⋂ l2]

should do the job handily, and look at 10.x (if you're on it) functions like ContainsAll, etc. for alternative ways of doing the same.

Edit: BTW - I assumed sorted lists w/o duplicates, as in your example. If they're not sorted,

 f[l1_, l2_] := MemberQ[Sort /@ {l1, l2, {}}, l1 ⋂ l2]

takes care of that.

If duplication in a list is allowed, you'll need to describe how that's to be handled.

If the lists are as in the OP (strictly sequential integers with no duplication), this will be goofy fast on large lists:

f3[l1_, l2_] := 
 OrderedQ[{l1[[1]], l2[[1]], l2[[-1]], l1[[-1]]}] || 
  OrderedQ[{l2[[1]], l1[[1]], l1[[-1]], l2[[-1]]}] || 
   (l1[[1]] > l2[[-1]] || l2[[1]] > l1[[-1]])
  • $\begingroup$ Actually, I find such a solution even better than a black box function. $\endgroup$
    – Kagaratsch
    May 19, 2016 at 21:46
  • 1
    $\begingroup$ Very elegant solution. +1. $\endgroup$ May 19, 2016 at 23:28
  • 2
    $\begingroup$ @LeonidShifrin: A +1 from LS? Like the Pope inviting me to a bible reading. Appreciated! $\endgroup$
    – ciao
    May 19, 2016 at 23:42
  • 1
    $\begingroup$ Lol. I should make a fake new account and express my delight with others' answers using that one :). Seriously, though, your code is as close to a mathematical solution as it gets, and at the same time idiomatic, brief and using only the core language (the code using new functions is nice, but I prefer your version, because it exposes internals more). This is the type of code that the intersection of math and programming should make possible. Unfortunately, it is hard to get this level of expressiveness in the majority of cases. $\endgroup$ May 19, 2016 at 23:49

For this one, you will need 10.2 or higher (ContainsAll was introduced in 10.2):

f[l1_, l2_] := Or[
 DisjointQ[l1, l2],
 ContainsAll[l1, l2],
 ContainsAll[l2, l1]

Including this solution because it might be easier to read this code since it closely encodes your written description.

  • $\begingroup$ Nice one. I wasn't aware of these, good to know. +1 $\endgroup$ May 19, 2016 at 23:29

g = Through @* Or[ContainsNone, ContainsOnly, ContainsAll];

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


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


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


list =
  {{{1, 2}, {3, 4, 5}},
   {{1, 2, 3}, {2, 3}},
   {{1, 2, 3}, {3, 4}}};

With UniqueElements (new in 13.1)

f[{a_, b_}] := 
 With[{u = UniqueElements[{a, b}]}, u[[1]] == a || u[[2]] == {}]

f /@ list

{True, True, False}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.