Wolfram Research

Function Repository Resource:

MapAtKey

Source Notebook

Apply functions to specific keys in an association

Contributed by: Bob Sandheinrich

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]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information