Function Repository Resource:

DeleteCasesFrom

Source Notebook

Remove all elements from the value of a symbol that match a pattern and reset the symbol to the result

Contributed by: Ian Ford

ResourceFunction["DeleteCasesFrom"][x,pattern]

removes all elements of the value of x that match pattern and resets x to the result.

ResourceFunction["DeleteCasesFrom"][x,pattern,levelspec]

removes all parts of the value of x on levels specified by levelspec that match pattern and resets x to the result.

ResourceFunction["DeleteCasesFrom"][x,pattern,levelspec,n]

removes the first n parts of the value of x that match pattern and resets x to the result.

Details and Options

ResourceFunction["DeleteCasesFrom"][x,pattern] is equivalent to x=DeleteCases[x,pattern].
ResourceFunction["DeleteCasesFrom"] has attribute HoldFirst.
When used on an Association, ResourceFunction["DeleteCasesFrom"] deletes elements according to their values.
With the option HeadsTrue, you can delete heads with ResourceFunction["DeleteCasesFrom"]. Deleting the head of a particular element from x sets it equal to the result of applying FlattenAt to the value of x at that point.
ResourceFunction["DeleteCasesFrom"] uses standard level specifications.
The default value for levelspec in ResourceFunction["DeleteCasesFrom"] is {}.
A positive level n consists of all parts of the value of x specified by n indices.
A negative level -n consists of all parts of the value of x with depth n.
Level -1 consists of numbers, symbols and other objects that do not have subparts.
Level 0 corresponds to the whole expression.
ResourceFunction["DeleteCasesFrom"] traverses the parts of the value of x in a depth-first order, with leaves visited before roots.

Examples

Basic Examples (4) 

Set s equal to a List:

In[1]:=
s = {1, 1, x, 2, 3, y, 9, y}
Out[1]=

Delete cases that match integers:

In[2]:=
ResourceFunction["DeleteCasesFrom"][s, _Integer]
Out[2]=

The value assigned to s has changed:

In[3]:=
s
Out[3]=

Delete elements from an Association:

In[4]:=
s = <|"a" -> 1, "b" -> 2, c -> "3", d -> 4|>
Out[4]=
In[5]:=
ResourceFunction["DeleteCasesFrom"][s, _Integer]
Out[5]=

DeleteCasesFrom can access parts of the elements of an Association:

In[6]:=
s = <|"a" -> 1, "b" -> 2, c -> {"3", 4}, d -> 4|>
Out[6]=
In[7]:=
ResourceFunction["DeleteCasesFrom"][s, _Integer, {2}]
Out[7]=

Delete elements from a nested Association:

In[8]:=
s = <|"a" -> 1, "b" -> <|"3" -> 3, "4" -> "4", "5" -> <|f -> 1|>|>, "c" -> 4|>
Out[8]=
In[9]:=
ResourceFunction["DeleteCasesFrom"][s, _Integer, {2, 3}]
Out[9]=

Scope (2) 

Delete elements matching an Association from a List:

In[10]:=
s = {<|a -> 1|>, <|b -> 2|>}
Out[10]=
In[11]:=
ResourceFunction["DeleteCasesFrom"][s, <|a -> _|>]
Out[11]=

Delete elements from a List matching a condition:

In[12]:=
s = {<|a -> 1|>, <|b -> 2|>}
Out[12]=
In[13]:=
ResourceFunction["DeleteCasesFrom"][s, <|_ -> a_ /; EvenQ[a]|>]
Out[13]=
In[14]:=
ResourceFunction["DeleteCasesFrom"][s, <|_ -> a_ /; OddQ[a]|>]
Out[14]=

Options (1) 

Heads (1) 

Deleting the head f effectively flattens the expression:

In[15]:=
s = {1, f[2, 3], 4}
Out[15]=
In[16]:=
ResourceFunction["DeleteCasesFrom"][s, f, {2}, Heads -> True]
Out[16]=

Possible Issues (2) 

Deleting the head effectively flattens the expression:

In[17]:=
s = {1, f[2, 3], 4}
Out[17]=
In[18]:=
ResourceFunction["DeleteCasesFrom"][s, f, {2}, Heads -> True]
Out[18]=

Deleting the head in an Association removes the entry:

In[19]:=
s = <|"a" -> 1, "b" -> f["2", 3], "c" -> 4|>
Out[19]=
In[20]:=
ResourceFunction["DeleteCasesFrom"][s, f, {2}, Heads -> True]
Out[20]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 29 November 2018

License Information