MapAt
Details
- MapAt[f,expr,{i,j,…}] or MapAt[f,expr,{{i,j,…}}] applies f to the part expr[[i,j,…]].
- The list of positions used by MapAt is in the same form as is returned by the function Position. »
- MapAt works on Association objects, using the same specification for keys as in Part.
- MapAt applies f repeatedly to a particular part if that part is mentioned more than once in the list of positions. »
- MapAt[f,pos][expr] is equivalent to MapAt[f,expr,pos].
Examples
open all close allBasic Examples (5)
Map f onto the part at position 2:
MapAt[f, {a, b, c, d}, 2]MapAt[f, {a, b, c, d}, {{1}, {4}}]Map f onto a more deeply nested part:
MapAt[f, {{a, b, c}, {d, e}}, {2, 1}]Map f onto the second element of all top-level parts (the "second column"):
MapAt[f, {{a, b, c}, {d, e}}, {All, 2}]Use the operator form of MapAt:
MapAt[f, 1][{a, b, c, d}]Scope (12)
Associations (3)
MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|>, 3]Use Key to specify position:
MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, Key["b"]]For string keys, Key is not needed:
MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, "b"]Use negative position in an association:
MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, -3]Part Specifications (9)
A single element of the expression:
MapAt[h, {{a, b, c}, {d, e}, f, g}, 2]Negative numbers count from the end:
MapAt[h, {{a, b, c}, {d, e}, f, g}, -3]MapAt[h, {{a, b, c}, {d, e}, f, g}, {2, 1}]MapAt[h, {{a, b, c}, {d, e}, f, g}, {{2}, {1}}]MapAt[h, {{a, b, c}, {d, e}, f, g}, {{1, 1}, {2, 2}, {3}}]Map using Span syntax:
MapAt[f, {1, 2, 3, 4, 5, 6}, 3 ;; 4]Map f at a more deeply nested part:
MapAt[f, <|"a" -> 1, "b" -> {2, 3}, "c" -> 4, "d" -> 5|>, {"b", 1}]MapAt[f, <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4|>, {{"a"}, {"b"}}]MapAt[f, <|1 -> x ^ 2 + y ^ 2|>, {{Key[1], 1, 1}, {Key[1], 2, 1}}]Generalizations & Extensions (4)
MapAt can be used on expressions with any head:
MapAt[f, a + b + c + d, 2]MapAt[f, x ^ 2 + y ^ 2, {{1, 1}, {2, 1}}]Key and integer part specifications in associations are different:
MapAt[f, <|3 -> "a", 2 -> "b", 1 -> "c"|>, {{1}, {Key[1]}}]The function can be mapped onto the heads as well:
MapAt[f, {a, b, c}, 0]Map can be used on sparse arrays:
SparseArray[{1 -> 1, 2 -> 2, 10 -> 10}]MapAt[f, %, {{1}, {5}, {10}}]Applications (4)
MapAt[x&, {{a, b, c}, {d, e, f}}, {2, 1}]Reverse the sign of an element in a matrix:
MapAt[-#&, {{a, b, c}, {d, e, f}}, {2, 1}]Enumerate cases with f mapped onto successive elements:
Table[MapAt[f, {a, b, c, d}, i], {i, 4}]Reverse the color of a cell at a random position at each step:
ArrayPlot[NestList[MapAt[1 - #&, #, RandomInteger[{1, 30}]]&, Table[0, {30}], 20], Mesh -> All]Properties & Relations (2)
MapAt can use lists of parts returned by Position:
t = {x, x ^ 2, y, y ^ 2, z ^ 3};pos = Position[t, _ ^ _]MapAt[f, t, pos]These are the subexpressions returned by Extract:
Extract[t, pos]MapAt applies f repeatedly if a part is mentioned repeatedly:
MapAt[f, {a, b, c, d}, {{1}, {3}, {1}}]See Also
Map ReplaceAt MapIndexed ReplacePart Delete FlattenAt
Function Repository: AssociationMapAt MapAtKey MapAtEvaluate
Tech Notes
Related Links
History
Introduced in 1988 (1.0) | Updated in 2014 (10.0)
Text
Wolfram Research (1988), MapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/MapAt.html (updated 2014).
CMS
Wolfram Language. 1988. "MapAt." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/MapAt.html.
APA
Wolfram Language. (1988). MapAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MapAt.html
BibTeX
@misc{reference.wolfram_2026_mapat, author="Wolfram Research", title="{MapAt}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/MapAt.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_mapat, organization={Wolfram Research}, title={MapAt}, year={2014}, url={https://reference.wolfram.com/language/ref/MapAt.html}, note=[Accessed: 13-June-2026]}