1
$\begingroup$

Is there any way to ask a boolean question if the elements of the list {2,5} are in the list {1,2,3,4,5}?

$\endgroup$
6
  • 1
    $\begingroup$ MemberQ[{1, 2, 3, 4, 5}, #] & /@ {2, 5}? $\endgroup$
    – Öskå
    Commented Jun 2, 2014 at 12:21
  • $\begingroup$ Is there a way to code that without using shorthand notation. I've had some problems using shorthand notation when writing longer pieces of code; I'm not very experienced yet. $\endgroup$
    – Kaisey
    Commented Jun 2, 2014 at 12:23
  • $\begingroup$ You can still use Table: Table[MemberQ[{1, 2, 3, 4, 5}, i], {i, {2, 5}}]. $\endgroup$
    – Öskå
    Commented Jun 2, 2014 at 12:28
  • $\begingroup$ Do you need to test whether all elements of {2,5} are in the list or whether any one is present in the list? $\endgroup$
    – Szabolcs
    Commented Jun 2, 2014 at 13:06
  • $\begingroup$ I need if all of the elements of {2,5} are in the list. $\endgroup$
    – Kaisey
    Commented Jun 2, 2014 at 13:47

2 Answers 2

4
$\begingroup$

Use Intersection for good performance.

Union[{2,5}] === Intersection[{2,5},{1,2,3,4,5}]
$\endgroup$
1
$\begingroup$
list = {1, 2, 3, 4, 5};

e = {2, 5};

Using ContainsAll (new in 10.2)

ContainsAll[list, e]

True

$\endgroup$

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.