Function Repository Resource:

DeleteFrom

Source Notebook

Delete an element from the value of a symbol and reset the symbol to the result

Contributed by: Ian Ford

ResourceFunction["DeleteFrom"][x,n]

deletes the element at position n in the value of x, and resets x to the result. If n is negative, the position is counted from the end.

ResourceFunction["DeleteFrom"][x,{i,j,}]

deletes the part at position {i,j,} from x.

ResourceFunction["DeleteFrom"][x,{{i1,j1,},{i2,j2,}}]

deletes parts at several positions.

Details and Options

ResourceFunction["DeleteFrom"][x,pos] is equivalent to x=Delete[x,pos].
ResourceFunction["DeleteFrom"] has attribute HoldFirst.
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.
Deleting the head from x makes the head be Sequence.
Deleting position {} from x sets the value to Sequence[].
ResourceFunction["DeleteFrom"] works on SparseArray objects.
ResourceFunction["DeleteFrom"] works on associations, removing elements with the specified keys and returning them.

Examples

Basic Examples (3) 

Assign x to a list:

In[1]:=
x = Alphabet[]
Out[1]=

Delete the element at position 3 from the list:

In[2]:=
ResourceFunction["DeleteFrom"][x, 3]
Out[2]=

The value assigned to x has changed:

In[3]:=
x
Out[3]=

Delete at position 2 from the end:

In[4]:=
ResourceFunction["DeleteFrom"][x, -2]
Out[4]=

Delete at positions 1 and 3:

In[5]:=
ResourceFunction["DeleteFrom"][x, {{1}, {3}}]
Out[5]=

Delete from a 2D array:

In[6]:=
x = {{a, b}, {c, d}}
Out[6]=
In[7]:=
ResourceFunction["DeleteFrom"][x, {2, 1}]
Out[7]=

Delete from an Association:

In[8]:=
x = <|a -> b, "a" -> c|>
Out[8]=
In[9]:=
ResourceFunction["DeleteFrom"][x, "a"]
Out[9]=
In[10]:=
ResourceFunction["DeleteFrom"][x, Key[a]]
Out[10]=

Scope (1) 

DeleteFrom works on sparse arrays:

In[11]:=
x = SparseArray[{{i_, i_} -> i}, 3]
Out[11]=
In[12]:=
ResourceFunction["DeleteFrom"][x, 1]
Out[12]=

Properties and Relations (3) 

Deleting the head makes the head be Sequence:

In[13]:=
x = {a, b, c}
Out[13]=
In[14]:=
ResourceFunction["DeleteFrom"][x, 0]
Out[14]=

Deleting at an empty list of positions does not change the value:

In[15]:=
x = h[a, b]
Out[15]=
In[16]:=
ResourceFunction["DeleteFrom"][x, {}]
Out[16]=

Deleting at position {} deletes the whole expression:

In[17]:=
x = h[a, b]
Out[17]=
In[18]:=
ResourceFunction["DeleteFrom"][x, {{}}]
Out[18]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 17 December 2018

License Information