# Pop

Contributed by: Ian Ford

Remove and return an element of an expression

 ResourceFunction["Pop"][x] deletes the last element in the value of x, returns it and resets x to the result. ResourceFunction["Pop"][x,n] pops the element at position n from x. If n is negative, the position is counted from the end. ResourceFunction["Pop"][x,{i,j,…}] pops the part at position {i,j,…}. ResourceFunction["Pop"][x,{{i1,j1,…},{i2,j2,…}}] pops parts at several positions.

## Details and Options

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

## Examples

### Basic Examples

Assign x to a list:

 In[1]:=
 Out[1]=

Pop the last element of the list:

 In[2]:=
 Out[2]=

The value assigned to x has changed:

 In[3]:=
 Out[3]=

Pop the element at position 3:

 In[4]:=
 Out[4]=
 In[5]:=
 Out[5]=

Pop at position 2 from the end:

 In[6]:=
 Out[6]=
 In[7]:=
 Out[7]=

Pop at positions 1 and 3:

 In[8]:=
 Out[8]=
 In[9]:=
 Out[9]=

Pop from a 2D array:

 In[10]:=
 Out[10]=
 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=

Pop from an association:

 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=
 In[15]:=
 Out[15]=
 In[16]:=
 Out[16]=
 In[17]:=
 Out[17]=

### Scope

Pop works on sparse arrays:

 In[18]:=
 Out[18]=
 In[19]:=
 Out[19]=
 In[20]:=
 Out[20]=

### Properties and Relations

Popping the head of a whole expression makes the head be Sequence:

 In[21]:=
 Out[21]=
 In[22]:=
 Out[22]=
 In[23]:=
 Out[23]=

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

 In[24]:=
 Out[24]=
 In[25]:=
 Out[25]=
 In[26]:=
 Out[26]=

Popping at position {} pops the whole expression:

 In[27]:=
 Out[27]=
 In[28]:=
 Out[28]=
 In[29]:=
 Out[29]=