WolframLang: Check Item Exist in List

By Xah Lee. Date: . Last updated: .
MemberQ
  • MemberQ[ expr, pattern ]
  • MemberQ[ expr, pattern, levelspec ] → limit to Level Spec.

Check existence by Pattern.

default Level Spec is {1}

MemberQ

MemberQ[ {1, 2, 3}, 2 ]

(* True *)

MemberQ[ {1, {a, b}, 3}, {a, b} ]

(* True *)
(* using a pattern *)

MemberQ[ {1, {a, b}, 3}, {_, _} ]
(* True *)

MemberQ[ {1, {a, b}, 3}, _Integer ]
(* True *)
Count
  • Count[ expr, pattern ]
  • Count[ expr, pattern, levelspec ] → limit to Level Spec.

Count by Pattern

default Level Spec is {1}

Count

Count[ {1, 2, 3, 1}, 1 ]

(* 2 *)

Count[ {1, {a, b}, 3}, {a, b} ]

(* 1 *)
(* using a pattern *)

Count[ {1, {a, b}, 3}, {_, _} ]

(* 1 *)

Count[ {1, {a, b}, 3}, _Integer ]

(* 2 *)
(* using a levelspec *)

Count[ {1,{2,{3,4}}}, {_, _} ]

(* 1 *)

Count[ {1,{2,{3,4}}}, {_, _}, {1,Infinity} ]

(* 2 *)

Get an Element's Position

WolframLang List Operations