Wolfram: Association (Key Value List)

By Xah Lee. Date: . Last updated: .

What is Association

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 • JS: Map (aka Dictionary)Python: DictionaryPerl: Hash TableJava: MapElisp: Association List

Association Syntax

Association[rule1, rule2, etc]

🔸 SHORT SYNTAX: <|args|>

Association is an ordered list of (key and value) pairs.

Each rule is one of:

  • Rule[key, val]
  • RuleDelayed[key, val]

〔see Wolfram: Rule, RuleDelayed

  • The key can be any Expression.
  • The value can be any Expression.
  • If a key already exist, the earlier one is removed.

Association

(* create a association. 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 *)

Symbol keys

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

Integer keys

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

Arbitrary Expression as Keys

(* create a association, arbitrary expression as keys *)
xx = Association[ f[x] -> 3, Sin[b] -> 2 ]
(* <|f[x] -> 3, Sin[b] -> 2|> *)
Association[{args}]

same as Association[args]

Length

Length[ asso ]

number of items.

Length

Check If is Association

AssociationQ[expr]

return true if expr is an association.

xx = Association[ "a" -> 3, "b" -> 2 ];
AssociationQ[xx]
(* True *)

WolframLang, Association (Key Value List)