Insert
Details
- If the position n is given as a positive integer, the new element is inserted before the element at position n. If n is negative, the new element is inserted after the existing one. »
- list can have any head, not necessarily List. »
- If expr has length l, Insert[expr,elem,l+1] appends elem at the end. Conversely, Insert[expr,elem,-l-1] prepends elem at the beginning. »
- If expr is an Association object, Insert[expr,key->value,pos] inserts an element in the association.
- When inserting into an association, positions may additionally be given as Key[k] as well as the short form "key". »
- If the key of the element to be inserted is already present, then the old element with the same key is deleted. »
- Insert works on SparseArray objects by effectively inserting into the corresponding ordinary lists.
- Insert[elem,pos][expr] is equivalent to Insert[expr,elem,pos].
Examples
open all close allBasic Examples (5)
Insert[{a, b, c, d, e}, x, 3]Insert x after position 2 from the end:
Insert[{a, b, c, d, e}, x, -2]Insert x before the first element, before the third element and after the last element:
Insert[{a, b, c, d, e}, x, {{1}, {3}, {-1}}]For associations, positions can be specified using Key:
Insert[<|a -> 1, b -> 2|>, c -> 3, Key[a]]Insert into an expression with any head:
Insert[f[a, b, c, d], x, 2]Scope (11)
Position Specifications (5)
Insert into a single, deeper position:
Insert[{{a, b}, {c, d}}, x, {1, 2}]Insert at any position in any expression:
1 + x ^ (a + b) + y ^ (c + d)Insert[%, zzz, {3, 2, 3}]Insert at position {1,3} in a matrix, making the first row four elements long:
mat = {{a, b, c}, {d, e, f}, {g, h, i}};Insert[mat, x, {1, 3}]Insert before the first and third parts (rows) of a matrix:
Insert[mat, x, {{1}, {3}}]Insert several elements into the matrix, creating a 3×4 matrix:
Insert[mat, x, {{1, 2}, {3, 3}, {2, 1}}]Define an operator form of Insert and use it on multiple expressions:
op = Insert[x, 3];op /@ {Range[3], {a, b, c}, {i, j, k, l}}Mix integer parts and Key[key] parts in a single specification:
Insert[{<|a -> 1, b -> 2|>, <|a -> 2, b -> 4|>}, x -> Infinity, {1, Key[a]}]Associations (6)
For string keys, the Key wrapper is optional:
Insert[<|"a" -> 1, "b" -> 2|>, "c" -> 3, "b"]% === Insert[<|"a" -> 1, "b" -> 2|>, "c" -> 3, Key["b"]]Insert before the second position:
Insert[<|2 -> {a, b}, 1 -> {c, d}|>, 3 -> {e, f}, 2]Insert[<|2 -> {a, b}, 1 -> {c, d}|>, 3 -> {e, f}, Key[2]]Insert[<|a -> 1, b -> 2|>, {c -> 3, d -> 4}, Key[a]]Inserting entries with repeating keys in an Association overrides earlier values:
Insert[<|a -> 1, b -> 2|>, a -> 3, Key[a]]Insert after the last element:
Insert[<|1 -> a, 2 -> b|>, 3 -> c, -1]Insert[<|1 -> {a, b}, 2 -> {c, d}|>, x, {2, 1}]Only a rule or list of rules can be inserted into the first level of an association:
Insert[<|a -> 1, b -> 2|>, foo, Key[a]]Applications (1)
Properties & Relations (7)
If the insertion position is positive, the new element is inserted before the existing element:
Insert[{a, b, c}, x, 2]If the position is negative, the new element comes after the existing element:
Insert[{a, b, c}, x, -2]Valid positive positions range from 1 to Length[expr]+1:
expr = {1, 2, 3};Table[Insert[expr, x, k], {k, 1, Length[expr] + 1}]Valid negative positions range from -1 down to -Length[expr]-1:
Table[Insert[expr, x, k], {k, -1, -Length[expr] - 1, -1}]Insertion positions always refer to positions in the original expression:
Insert[{a, b, c}, x, {{1}, {3}}]Thus, only the first insertion is guaranteed to appear at the specified insertion position:
Position[%, x]If a position is repeated, multiple insertions will occur at that position:
Insert[{a, b, c}, x, {{1}, {1}, {1}}]This can be used to surround an existing element with a new element:
Insert[{a, b, c}, x, {{1}, {-3}}]Insert[elem,1] is equivalent to Prepend[elem]:
list = {a, b, c};
Insert[x, 1][{a, b, c}] === Prepend[x][{a, b, c}]Insert[elem,-1] is equivalent to Append[elem]:
list = {a, b, c};
Insert[x, -1][{a, b, c}] === Append[x][{a, b, c}]Insert takes position specifications as returned by Position:
expr = {1, two, <|a -> {3}|>};Position[expr, _Integer]Insert[expr, "an integer follows", %]Related Links
History
Introduced in 1988 (1.0) | Updated in 2003 (5.0) ▪ 2014 (10.0)
Text
Wolfram Research (1988), Insert, Wolfram Language function, https://reference.wolfram.com/language/ref/Insert.html (updated 2014).
CMS
Wolfram Language. 1988. "Insert." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/Insert.html.
APA
Wolfram Language. (1988). Insert. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Insert.html
BibTeX
@misc{reference.wolfram_2026_insert, author="Wolfram Research", title="{Insert}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Insert.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_insert, organization={Wolfram Research}, title={Insert}, year={2014}, url={https://reference.wolfram.com/language/ref/Insert.html}, note=[Accessed: 12-June-2026]}