Function Repository Resource:

AppendAt

Source Notebook

Append to the specified element or after the last element at a specified level of an expression

Contributed by: Wolfram Staff

ResourceFunction["AppendAt"][expr,elem,level]

gives expr with elem appended to the end of the last expression at depth level.

ResourceFunction["AppendAt"][expr,elem,{k1,k2,}]

appends elem to the part of expr at the position specified by indices ki.

ResourceFunction["AppendAt"][assoc,elem,{key}]

appends elem to the value corresponding to the specified key in the association.

Details

ResourceFunction["AppendAt"][list,elem,1] is equivalent to Append[list,elem].
key can be a string or an arbitrary expression in the form Key[key].

Examples

Basic Examples (4) 

Append to the last part of the expression at the specified level:

In[1]:=
ResourceFunction["AppendAt"][{{1, 2}, {3, 4}}, X, 2]
Out[1]=

Append to the element of the expression at the specified position:

In[2]:=
ResourceFunction["AppendAt"][{{1, 2}, {3, 4}}, X, {2, 2}]
Out[2]=

Append to the values at the specified string key in the association:

In[3]:=
ResourceFunction["AppendAt"][<|"a" -> 1, "b" -> 2|>, X, {"b"}]
Out[3]=

Specify an arbitrary key:

In[4]:=
ResourceFunction["AppendAt"][<|a -> 1, b -> 2|>, X, {Key[b]}]
Out[4]=

Properties and Relations (1) 

For List expressions, AppendAt at level 1 is equivalent to Append:

In[5]:=
ResourceFunction["AppendAt"][{1, 2, 3}, X, 1]
Out[5]=
In[6]:=
Append[{1, 2, 3}, X]
Out[6]=

Version History

  • 1.0.0 – 19 April 2021

Related Resources

License Information