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 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 1 through n
    Infinitylevels 1 through Infinity
    {n}level n only
    {n1,n2}levels n1 through n2
  • The default value for levelspec in DeleteCases is {1}.
  • 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 -1 consists of numbers, symbols, and other objects that do not have subparts.
  • Level 0 corresponds to the whole expression.
  • DeleteCases traverses the parts of expr in a depth-first order, with leaves visited before roots.

Examples

open allclose all

Basic Examples  (5)

Delete cases that match integers:

Use the operator form:

Delete elements from Association:

DeleteCases can access parts of the elements of an Association:

Deleting elements from a nested Association:

Scope  (2)

Delete elements matching an Association from a list:

Delete elements from a list matching a condition:

Options  (1)

Heads  (1)

Deleting the head f effectively flattens the expression:

Applications  (1)

Build a function that works like Complement, but preserving order and multiplicity:

Possible Issues  (1)

Deleting the head effectively flattens the expression:

Deleting the head in Association removes the entry:

Wolfram Research (1991), DeleteCases, Wolfram Language function, https://reference.wolfram.com/language/ref/DeleteCases.html (updated 2014).

Text

Wolfram Research (1991), DeleteCases, Wolfram Language function, https://reference.wolfram.com/language/ref/DeleteCases.html (updated 2014).

CMS

Wolfram Language. 1991. "DeleteCases." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/DeleteCases.html.

APA

Wolfram Language. (1991). DeleteCases. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/DeleteCases.html

BibTeX

@misc{reference.wolfram_2024_deletecases, author="Wolfram Research", title="{DeleteCases}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/DeleteCases.html}", note=[Accessed: 22-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_deletecases, organization={Wolfram Research}, title={DeleteCases}, year={2014}, url={https://reference.wolfram.com/language/ref/DeleteCases.html}, note=[Accessed: 22-November-2024 ]}