WolframLang: Association, Add Key
Add One Key/Val Pair by Assignment Syntax
Tip: by WolframLang naming convention, function whose name ends in “To” or “From” modifies a variable.
assocVar[key]=val
- Add a key value pair to assocVar. If the key exist, value is updated. assocVar must be a variable.
Append Key/Value and Modify Variable
AssociateTo[ assocVar, rule ]
-
- Add a entry to a assoc variable and modify the variable. Return the updated value.
- If the key exist, value is updated.
xx = Association[ a -> 3 ]; AssociateTo[ xx, {c -> 4} ]; xx === <|a -> 3, c -> 4|>
AssociateTo[ assocVar, {rule1, rule2 etc} ]
-
Add multiple entries.
xx = Association[ a -> 3 ]; AssociateTo[ xx, {c -> 4, m -> 9} ]; xx === <|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[ assocVar, rule ]
- Append one or more entries to a assoc variable.
Use list for more than one.
If the key exist, value is updated.
AppendToxx = Association[ a -> 3, b -> 2 ]; AppendTo[ xx, {b -> 4, m -> 1} ]; xx == <|a -> 3, b -> 4, m -> 1|>
(* AppendTo works the same way as AssociateTo *) xx = Association[ a -> 3 ]; AppendTo[ xx, b -> 4 ]; yy = Association[ a -> 3 ]; AssociateTo[ yy, b -> 4 ]; xx === yy