Wolfram Computation Meets Knowledge

MapAtKey

Contributed by: Bob Sandheinrich

Apply functions to specific keys in an association

ResourceFunction["MapAtKey"][{key1f1,},assoc]

gives an association with each fi applied to the value of keyi in assoc.

Details and Options

Keys not included in the rules will maintain the same values in the result.

Examples

Basic Examples

Selectively modify values in an association:

In[1]:=
ResourceFunction[
 "MapAtKey"][{"a" -> (#^2 &), "b" -> (#/2 &)}, <|"a" -> 10, "b" -> 2, "c" -> 3|>]
Out[1]=

Properties and Relations

All specified keys must exist in the association:

In[2]:=
ResourceFunction[
 "MapAtKey"][{"a" -> (#^2 &), "d" -> (#/2 &)}, <|"a" -> 10, "b" -> 2, "c" -> 3|>]
Out[2]=

Resource History