DeleteCases

DeleteCases[expr,pattern]
removes all elements of expr that match pattern.

DeleteCases[expr,pattern,levelspec]
removes all parts of expr on levels specified by levelspec that match pattern.

DeleteCases[expr,pattern,levelspec,n]
removes the first n parts of expr that match pattern.

DeleteCases[pattern]
represents an operator form of DeleteCases that can be applied to an expression.

Details and OptionsDetails and Options

  • When used on an Association, DeleteCases deletes elements according to their values.
  • DeleteCases[pattern][expr] is equivalent to DeleteCases[expr,pattern].
  • With the option Heads->True, you can delete heads with DeleteCases. Deleting the head of a particular element in an expression is equivalent to applying FlattenAt to the expression at that point.
  • DeleteCases uses standard level specifications:
  • nlevels through n
    Infinitylevels through Infinity
    {n}level n only
    {n1,n2}levels through
  • The default value for levelspec in DeleteCases is .
  • A positive level n consists of all parts of expr specified by n indices.
  • A negative level -n consists of all parts of expr with depth n.
  • Level consists of numbers, symbols, and other objects that do not have subparts.
  • Level corresponds to the whole expression.
  • DeleteCases traverses the parts of expr in a depth-first order, with leaves visited before roots.

ExamplesExamplesopen allclose all

Basic Examples  (5)Basic Examples  (5)

Delete cases that match integers:

In[1]:=
Click for copyable input
Out[1]=

Use the operator form:

In[1]:=
Click for copyable input
Out[1]=

Delete elements from Association:

In[1]:=
Click for copyable input
Out[1]=

DeleteCases can access parts of the elements of an Association:

In[1]:=
Click for copyable input
Out[1]=

Deleting elements from a nested Association:

In[1]:=
Click for copyable input
Out[1]=
Introduced in 1991
(2.0)
| Updated in 2014
(10.0)