Function Repository Resource:

ElementQ

Source Notebook

Test if an element is contained in a list

Contributed by: Taliesin Beynon

ResourceFunction["ElementQ"][elem,list]

gives True if the value elem is present in list.

ResourceFunction["ElementQ"][list]

is the operator form of ResourceFunction["ElementQ"].

Details and Options

elem must be at level 1 in the list.
list can have any head, not necessarily List.
elem can be any expression.
ResourceFunction["ElementQ"] does not use patterns or functions for testing.

Examples

Basic Examples (5) 

Basic usage:

In[1]:=
ResourceFunction["ElementQ"][1, Range[5]]
Out[1]=

Operator form:

In[2]:=
ResourceFunction["ElementQ"][{1, 2, 3, 4, 5}][1]
Out[2]=

Test strings:

In[3]:=
ResourceFunction["ElementQ"]["Æ", CharacterRange[150, 200]]
Out[3]=

Test algebraic expressions:

In[4]:=
ResourceFunction["ElementQ"][x^2, Table[x^n, {n, 6}]]
Out[4]=

Head can be any other than List:

In[5]:=
ResourceFunction["ElementQ"][b, f[a, b, c]]
Out[5]=

Properties and Relations (1) 

ElementQ[elem,list] is equivalent to MemberQ[list,elem]:

In[6]:=
MemberQ[Range[5], 1]
Out[6]=

Possible Issues (1) 

ElementQ tests only elements at level 1:

In[7]:=
ResourceFunction["ElementQ"][2, {1, {2}, 3, 4, 5}]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 20 December 2018

License Information