Skip to content

Contains ​

Whether a collection has an element structurally equal to v.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageMemberQ!
Contains(xs, v)TrueTrue if some element of xsxs is structurally equal to vv, else FalseFalse.
Typed signatures
TypePackageApplies
(collection<any>, element: any) -> booleancompute-enginealways

Domain: Collections

Examples

Basic #
live ↗
InContains([1,2,3],2)\mathrm{Contains}(\bigl\lbrack1, 2, 3\bigr\rbrack, 2)OutTrue\mathrm{True}
live ↗
InContains([1,2,3],5)\mathrm{Contains}(\bigl\lbrack1, 2, 3\bigr\rbrack, 5)OutFalse\mathrm{False}
live ↗
InContains([1,3,4,1,2],2)\mathrm{Contains}(\bigl\lbrack1, 3, 4, 1, 2\bigr\rbrack, 2)OutTrue\mathrm{True}

Nothing is in the empty list

live ↗
InContains([],x)\mathrm{Contains}(\bigl\lbrack\bigr\rbrack, x)OutFalse\mathrm{False}
Scope #

Symbolic elements

live ↗
InContains([x2,y2],x2)\mathrm{Contains}(\bigl\lbrack x^2, y^2\bigr\rbrack, x^2)OutTrue\mathrm{True}

Elements can themselves be lists

live ↗
InContains([[1,2],[3,4]],[1,2])\mathrm{Contains}(\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 4\bigr\rbrack\bigr\rbrack, \bigl\lbrack1, 2\bigr\rbrack)OutTrue\mathrm{True}

Only the top-level elements are searched

live ↗
InContains([[1,2],[3,4]],1)\mathrm{Contains}(\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack3, 4\bigr\rbrack\bigr\rbrack, 1)OutFalse\mathrm{False}

A lazy Range is searched without being materialised

live ↗
InContains(1..100,50)\mathrm{Contains}(1..100, 50)OutTrue\mathrm{True}

Only top-level elements are searched, so the nested 0 is not found

live ↗
InContains([[1,1,3,0],[2,1,2,2]],0)\mathrm{Contains}(\bigl\lbrack\bigl\lbrack1, 1, 3, 0\bigr\rbrack, \bigl\lbrack2, 1, 2, 2\bigr\rbrack\bigr\rbrack, 0)OutFalse\mathrm{False}
Properties #

Compared after canonical ordering, so b+ab + a matches a+ba + b

live ↗
InContains([a+b],b+a)\mathrm{Contains}(\bigl\lbrack a+b\bigr\rbrack, b+a)OutTrue\mathrm{True}
Possible issues #

An exact and an approximate number with the same value match

live ↗
InContains([12],0.5)\mathrm{Contains}(\bigl\lbrack\frac{1}{2}\bigr\rbrack, 0.5)OutTrue\mathrm{True}

Repeated elements still count once: just a True/False answer

live ↗
InContains([1,2,1,3],1)\mathrm{Contains}(\bigl\lbrack1, 2, 1, 3\bigr\rbrack, 1)OutTrue\mathrm{True}
About
Examples12
Checked againstWolfram Language 11 agree1 differ
CrosswalkWolfram Language

See also: Any