Prepend
Details
- Prepend works on SparseArray objects, returning ordinary lists if necessary. »
- Prepend can be used on Association objects. Single new elements are specified in the form key->value. New values replace older ones if their keys are the same. »
- Multiple elements to prepend to an association can be specified in the form {key1->value1,key2->value2, …}. »
- Prepend[elem][expr] is equivalent to Prepend[expr,elem]. »
Examples
open all close allBasic Examples (3)
Prepend[{a, b, c, d}, x]Prepend[(| | |
| - | - |
| a | b |
| c | d |), {1, 2}]//MatrixFormPrepend to an Association:
Prepend[<|1 -> a, 2 -> b|>, 3 -> d]Scope (4)
op = Prepend[{1, 2}];Prepend the list to a flat list, resulting in a nested list:
op[{a, b, c, d}]Prepend to a list of lists, producing a matrix:
op[{{a, b}, {c, d}, {e, f}}]%//MatrixFormsa = SparseArray[{{3, 3} -> 1}]Prepend a row to it, producing another sparse array:
Prepend[sa, {a, 0, b}]The sparse array will revert to a normal array if the array structure cannot be maintained:
Prepend[sa, a]Prepend to an expression with a general head:
Prepend[f[a, b, c], x + y]Prepend several rules to an Association; they are added sequentially and thus appear in reverse order:
Prepend[<|1 -> a, 2 -> b|>, {3 -> d, 4 -> e}]Compare with the behavior of prepending to a list of rules:
Prepend[{1 -> a, 2 -> b}, {3 -> d, 4 -> e}]Applications (4)
Prepend[{{a, b}, {c, d}}, {x, y}]//MatrixFormPrepend to each row in a matrix:
Map[Prepend[#, x]&, {{a, b}, {c, d}}]//MatrixFormMapThread[Prepend, {{{a, b}, {c, d}}, {x, y}}]//MatrixFormSuccessively prepend to a list:
NestList[Prepend[#, x]&, {a}, 5]Properties & Relations (4)
Prepend[list,x] is equivalent to Join[{x},list]:
Prepend[{a, b, c}, 1] === Join[{1}, {a, b, c}]This is true even when x is a list:
Prepend[{a, b, c}, {1}] === Join[{{1}}, {a, b, c}]For a general expression e, Prepend[e,x] is equivalent to Join[Head[e][x],e]:
With[{e = f[a, b, c]}, Prepend[e, 3] === Join[Head[e][3], e]]Prepend[x][e] is equivalent to Prepend[e,x]:
Prepend[x][{a, b, c}] === Prepend[{a, b, c}, x]When prepending to an association, if a key already exists, its value will be overwritten by the prepended value:
Prepend[<|1 -> a, 2 -> b|>, 2 -> c]Possible Issues (2)
There is no special behavior when the first and second arguments have the same head:
Prepend[{a, b, c}, {x, y}]It is possible to use Flatten after Prepend to effectively prepend several elements individually:
Flatten[%]Alternatively, use Join with the arguments reversed:
Join[{x, y}, {a, b, c}]When working on a shared list in parallel, Prepend is not atomic (or thread safe):
sl = {};SetSharedVariable[sl]ParallelDo[sl = Prepend[sl, i ^ 2], {i, 1, 8}]slUse PrependTo in such circumstances:
sl = {};SetSharedVariable[sl]ParallelDo[PrependTo[sl, i ^ 2], {i, 1, 8}]slRelated Links
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2014 (10.0)
Text
Wolfram Research (1988), Prepend, Wolfram Language function, https://reference.wolfram.com/language/ref/Prepend.html (updated 2014).
CMS
Wolfram Language. 1988. "Prepend." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/Prepend.html.
APA
Wolfram Language. (1988). Prepend. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Prepend.html
BibTeX
@misc{reference.wolfram_2026_prepend, author="Wolfram Research", title="{Prepend}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Prepend.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_prepend, organization={Wolfram Research}, title={Prepend}, year={2014}, url={https://reference.wolfram.com/language/ref/Prepend.html}, note=[Accessed: 13-June-2026]}