Testing and Searching List Elements

Position[list,form]the positions at which form occurs in list
Count[list,form]the number of times form appears as an element of list
MemberQ[list,form]test whether form is an element of list
FreeQ[list,form]test whether form occurs nowhere in list

Testing and searching for elements of lists.

"Getting Pieces of Lists" discusses how to extract pieces of lists based on their positions or indices. Mathematica also has functions that search and test for elements of lists, based on the values of those elements.

This gives a list of the positions at which appears in the list.
In[1]:=
Click for copyable input
Out[1]=
Count counts the number of occurrences of .
In[2]:=
Click for copyable input
Out[2]=
This shows that is an element of .
In[3]:=
Click for copyable input
Out[3]=
On the other hand, is not.
In[4]:=
Click for copyable input
Out[4]=
This assigns to be the 3×3 identity matrix.
In[5]:=
Click for copyable input
Out[5]=
This shows that does occur somewhere in .
In[6]:=
Click for copyable input
Out[6]=
This gives a list of the positions at which occurs in .
In[7]:=
Click for copyable input
Out[7]=

As discussed in "Finding Expressions That Match a Pattern", the functions Count and Position, as well as MemberQ and FreeQ, can be used not only to search for particular list elements, but also to search for classes of elements which match specific "patterns".

New to Mathematica? Find your learning path »
Have a question? Ask support »