Wolfram Research

Function Repository Resource:

AppendSequence

Source Notebook

Append multiple items to an expression at once

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["AppendSequence"][expr,elem1,elem2,]

gives expr with the elements elem1,elem2, appended.

ResourceFunction["AppendSequence"][elem]

represents an operator form of ResourceFunction["AppendSequence"] that can be applied to an expression.

Details and Options

In iteratively building a list, it is usually more efficient to use Sow and Reap than to use ResourceFunction["AppendSequence"][list,new1,new2,] at each step.
ResourceFunction["AppendSequence"] works on SparseArray objects, returning ordinary lists if necessary.
ResourceFunction["AppendSequence"] can be used on Association objects. Single new elements are specified in the form keyvalue. New values replace older ones if their keys are the same.
ResourceFunction["AppendSequence"][elem][expr] is equivalent to ResourceFunction["AppendSequence"][expr,elem].
ResourceFunction["AppendSequence"] has the attribute SequenceHold.
In ResourceFunction["AppendSequence"][h[],e1,e2,], if h has the SequenceHold attribute, the e1, e2, are appended as a single Sequence object in the output, yielding h[,Sequence[e1,e2,]].

Examples

Basic Examples

In[1]:=
ResourceFunction["AppendSequence"][{a, b, c, d}, x, y]
Out[1]=

Append to an Association:

In[2]:=
ResourceFunction["AppendSequence"][<|1 -> a, 2 -> b|>, 3 -> d, 4 -> e]
Out[2]=

Append to a SparseArray:

In[3]:=
array = SparseArray[{1 -> a, 5 -> b, 10 -> c}]
Out[3]=
In[4]:=
ResourceFunction["AppendSequence"][array, x, y, z]
Out[4]=

View the corresponding list:

In[5]:=
Normal[%]
Out[5]=

Use the operator form:

In[6]:=
ResourceFunction["AppendSequence"][x]
Out[6]=
In[7]:=
%[f[a, b]]
Out[7]=

Use a head other than List:

In[8]:=
ResourceFunction["AppendSequence"][f[a, b, c], x + y, z]
Out[8]=

Append elements without evaluating them:

In[9]:=
ResourceFunction["AppendSequence"][Hold[1 + 1], Unevaluated[2 + 2], Unevaluated[3 + 3]]
Out[9]=

Applications

Append multiple options to a Notebook:

In[10]:=
ResourceFunction["AppendSequence"][Notebook[{Cell["Hello", "Text"]}], Background -> LightBlue, FontColor -> Red]
Out[10]=
In[11]:=
NotebookPut[%]
Out[11]=

Successively append to a list:

In[12]:=
NestList[ResourceFunction["AppendSequence"][#, x, y] &, {a, b}, 5]
Out[12]=

Possible Issues

Using AppendSequence with heads that have the attribute SequenceHold will append a Sequence:

In[13]:=
ResourceFunction["AppendSequence"][HoldComplete[a, b], x, y]
Out[13]=

The operator form only takes a single argument:

In[14]:=
ResourceFunction["AppendSequence"][1, 2, 3][{x, y, z}]
Out[14]=

Wrap multiple arguments with Sequence to use in the operator form:

In[15]:=
ResourceFunction["AppendSequence"][Sequence[1, 2, 3]][{x, y, z}]
Out[15]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information