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 x after position 2 from the end:
Insert x before the first element, before the third element and after the last element:
For associations, positions can be specified using Key:
Scope (11)
Position Specifications (5)
Insert into a single, deeper position:
Insert at any position in any expression:
Insert at position {1,3} in a matrix, making the first row four elements long:
Insert before the first and third parts (rows) of a matrix:
Insert several elements into the matrix, creating a 3×4 matrix:
Define an operator form of Insert and use it on multiple expressions:
Mix integer parts and Key[key] parts in a single specification:
Associations (6)
For string keys, the Key wrapper is optional:
Insert before the second position:
Inserting entries with repeating keys in an Association overrides earlier values:
Insert after the last element:
Only a rule or list of rules can be inserted into the first level of an association:
Applications (1)
Properties & Relations (7)
If the insertion position is positive, the new element is inserted before the existing element:
If the position is negative, the new element comes after the existing element:
Valid positive positions range from 1 to Length[expr]+1:
Valid negative positions range from -1 down to -Length[expr]-1:
Insertion positions always refer to positions in the original expression:
Thus, only the first insertion is guaranteed to appear at the specified insertion position:
If a position is repeated, multiple insertions will occur at that position:
This can be used to surround an existing element with a new element:
Insert[elem,1] is equivalent to Prepend[elem]:
Insert[elem,-1] is equivalent to Append[elem]:
Insert takes position specifications as returned by Position:
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_2025_insert, author="Wolfram Research", title="{Insert}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/Insert.html}", note=[Accessed: 20-November-2025]}
BibLaTeX
@online{reference.wolfram_2025_insert, organization={Wolfram Research}, title={Insert}, year={2014}, url={https://reference.wolfram.com/language/ref/Insert.html}, note=[Accessed: 20-November-2025]}