WolframLang: Association (Key Value List)

By Xah Lee. Date: . Last updated: .

WolframLang's Association is a dedicated structure for list of key value pairs. It provides a efficient implementation of ordered list of pairs, when you have large number of pairs such as over a thousand pairs.

💡 TIP: Similar to • Elisp: Association ListJS: Map Object TutorialPython: DictionaryPerl: Hash TableJava: Map

Create Association

Association[rule1, rule2, etc]

🔸 SHORT SYNTAX: <|args|>

Association is an ordered list of key/value pairs.

Each rule is one of:

  • Rule[key, val] (🔸 SHORT SYNTAX: key -> val)
  • RuleDelayed[key, val] (🔸 SHORT SYNTAX: key :> val)

〔see WolframLang: Rule, RuleDelayed

  • The key can be any expression. Usually it's String or Symbol or integer.
  • The value can be any expression.
  • If a key already exist, the earlier one is removed.

Association

(* create a association list. string keys *)
xx = Association[ "a" -> 3, "b" -> 2 ]
(* <|a -> 3, b -> 2|> *)

(* short syntax *)
yy = <| "a" -> 3, "b" -> 2 |>
(* <|a -> 3, b -> 2|> *)

xx === yy
(* True *)
(* create a association list, symbol keys *)
xx = Association[ a -> 3, b -> 2 ]
(* <|a -> 3, b -> 2|> *)
(* create a association list, integer keys *)
xx = Association[ 5 -> 3, 4 -> 2 ]

(* 
output
<|5 -> 3, 4 -> 2|>
 *)
(* create a association list, arbitrary expression as keys *)
xx = Association[ f[x] -> 3, Sin[b] -> 2 ]

(* 
output
<|f[x] -> 3, Sin[b] -> 2|>
*)
Association[{args}]

same as Association[args]

Length

Length[ asso ]

number of items.

Length

WolframLang, Association (Key Value List)