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 (1) 

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 (1) 

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

Version History

  • 1.0.0 – 03 December 2018

License Information