WolframLang: Association, Add Key

By Xah Lee. Date: . Last updated: .

Add Item by Assignment Syntax

πŸ’‘ TIP: by WolframLang naming convention, function whose name ends in β€œTo” or β€œFrom” modifies a variable.

Append Item to Assoc Var by Assignment

assoVar[key]=val
  • Add a item to assoVar.
  • If the key exist, value is updated.
  • assoVar must be a variable.
  • Return val.
xx = Association[ "a" -> 3 ];
xx["c"] = 4
xx === Association["a" -> 3, "c" -> 4 ]

Append Item and Modify Variable

AssociateTo[ assoVar, rule ]
  • Add a rule to a assoc variable assoVar and modify the variable.
  • If the key exist, value is updated.
  • Return the updated value of asso var.

AssociateTo

xx = Association[ "a" -> 3 ]
result = (AssociateTo[ xx, {"c" -> 4} ])
xx === Association["a" -> 3, "c" -> 4 ]
result === Association["a" -> 3, "c" -> 4 ]
AssociateTo[ assoVar, listOfRules]

Add multiple items.

xx = Association[ a -> 3 ];
AssociateTo[ xx, {c -> 4, m -> 9} ];
xx === Association[ a -> 3, c -> 4, m -> 9]

AppendTo

AppendTo is usually for working with List, but it works with Association too. It works the same way as AssociateTo.

AppendTo

WolframLang: Association (Key Value List)