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$
  • 1
    $\begingroup$ MemberQ[{1, 2, 3, 4, 5}, #] & /@ {2, 5}? $\endgroup$ – Öskå Jun 2 '14 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 Jun 2 '14 at 12:23
  • $\begingroup$ You can still use Table: Table[MemberQ[{1, 2, 3, 4, 5}, i], {i, {2, 5}}]. $\endgroup$ – Öskå Jun 2 '14 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 Jun 2 '14 at 13:06
  • $\begingroup$ I need if all of the elements of {2,5} are in the list. $\endgroup$ – Kaisey Jun 2 '14 at 13:47
3
$\begingroup$

Use Intersection for good performance.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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